how to create file download link to a file stored in MYSQL database with PHP?

Hello Guys, Please I have a stuff here that requires upload, I instructed the uploaded document to go to a folder on the server but I noticed that it is not reflecting in my dbase here is the structure (

Upload Cv


Acceptable formats include: .doc and .pdf. ) but the cv goes into the directed path and nothing appears in the dbase column all it tells me is "undefined index in the hidden field stuff.

Secondly if this work out, How do I pull a link from the dbase that matches a file to the rightful owner

I trust you guys sense of judgement

Your post seems to be missing somethings like sample database structure, and code.

But nevertheless, if you have the uploaded the file successfully it should be in the tmp folder of the server.
You will need to use the php command,

move_uploaded_file(string $filename , string $destination);
To move it to the desired directory.

E.g
If you have a file input named “myfile”.

After the user hits upload your code should do something like:

$success = false;
$destination = ‘/uploads’;
$tmp_name = $_FILES[“myfile”][“tmp_name”];
if( file_exists($tmp_name) ){
//You should also check if the file name already exists in the destination folder before moving it to avoid overwriting any files.
$success = move_uploaded_file($tmp_name , $destination);
if($success){
//code to save file name to database here … $_FILES[“myfile”][“name”];
}
}

@doctorfox
This is my code
$cv_route = $_FILES[‘cv’][‘name’];
$today =date(“y-m-d” );

//upload image and check for image type
//make sure to change your path to match your images directory
$cvDir =“c:/wamp/www/wale/cv/”;
$cvname = $cvDir . $cv_route;
if (move_uploaded_file($_FILES[‘cv’][‘tmp_name’],$cvname))
{
//get info about the image being uploaded
list( $type) = getimagesize($cvname);
switch ($type) {
case 1:
$ext = “.pdf”;
break;
case 2:
$ext = “.doc”;
break;
default:
//echo “Sorry, but the file you uploaded was not a doc, pdf file.
”;
//echo “Please hit your browser’s ‘back’ button and try again.”;
}
the cv is going to the folder specified but not reflecting the name in the database. So what am looking for is a code that will add the name of the cv to the column created so that when the owner of the cv is selected using the “Select” statement, the CV will also be pulled along with clicked and can be downloaded. Tks

If I am thinking what you’re thinking, you want someone to write a query to save a field to your database table for you.
Surely, I could tell you
mysql_query(“INSERT INTO cvtable (‘filename’) VALUES ({$cvname})”);

Assuming the table is called cvtable.
Otherwise, you may be assuming there’s a telepathic coder here who knows what your database structure looks like without you posting anything about it.

First of all i think tell you something that u might not have done one is maybe you didnt specify multipart/form-data in your form, or u didnt give it a name thats usually the culprit i’ve spend a day debugging only to discover that there was no file recognized,always use Firebug to monitor HTTP headers. there you should see binary codes of the FILE being uploaded ciao.

<?php mysql_connect("localhost","root","") or die("cannot connect to the database server"); mysql_select_db("stressert") or die ("cannot select database"); ?> <?php //include("config.php"); ?> <?php $vcode=$_POST['vcode']; $fname=$_POST['fname']; $lname=$_POST['lname']; $email=$_POST['email']; $mobile=$_POST['mobile']; $phone=$_POST['phone']; $address=$_POST['address']; $address2=$_POST['address2']; $city=$_POST['city']; $state=$_POST['state']; $city=$_POST['city']; $zip=$_POST['zip']; $country=$_POST['country']; $countryborn=$_POST['countryborn']; $stateoforigin=$_POST['stateoforigin']; $dob=$_POST['dob']; $sex=$_POST['sex']; $empstatus=$_POST['empstatus']; $currsalary=$_POST['currsalary']; $skills=$_POST['skills']; $cv=$_POST['cv']; //variables access $cv_route = $_FILES['cv']['name']; $today =date("y-m-d" ); //upload image and check for image type //make sure to change your path to match your images directory $cvDir ="c:/wamp/www/Stresserts/cv/"; $cvname = $cvDir . $cv_route; if (move_uploaded_file($_FILES['cv']['tmp_name'],$cvname)) { //get info about the image being uploaded list( $type) = getimagesize($cvname); switch ($type) { case 1: $ext = ".pdf"; break; case 2: $ext = ".doc"; break; default: //echo "Sorry, but the file you uploaded was not a doc, pdf file.
"; //echo "Please hit your browser's 'back' button and try again."; } $query="INSERT INTO applicant(vcode,fname,lname,email,mobile,phone,address,address2,city,state,zip,country,countryborn,stateoforigin,dob,sex, empstatus, currsalary, skills, cv) VALUES ('$vcode','$fname','$lname','$email','$mobile','$phone','$address','$address2','$city','$state', '$zip','$country','$countryborn','$stateoforigin','$dob','$sex','$empstatus', '$currsalary', '$skills', '$cv')"; mysql_query($query) or die(mysql_error()); echo "submitted"; } ?>

Above here is my process code and below is my form

value="">
	  <td align="left">Upload Cv</td>
	  <td align="left"><input name="cv" type="file" id="cv">
	  <input type='hidden' name='cv' value="<?php echo @($_POST['cv'])?>">

Acceptable formats include: .doc and .pdf.




You can please check and see if you can help (I had to reduce the content due to space constraint, other fields in the form re submitting except the file upload that was why it is the only one i copied out here.

you put $cv u sure its not suppose to be $cvname , but even if it was $cv is a post varaible assuming u keep leaving that field empty its possible you aint seeing anything there cross check to make sure the $cv is $cvname which is suppose to be the dir path, if not mail me [email protected] my internet expires by 8

also ur directory is an absolute path make it relative if not your application will break when u take it online use “cv/” instead.

@dojoVader, the issue I have with the stuff is that it is not inserting the file name into the dbase, but it is saving the cv is the specified folder through the path stated, each time I test with a document, it uploads the document to the cv folder but I can see the name is the dbase. So what I want is to be see the file name in the dbase , so that when the report is generated I should be able to download the cv. Thanks bro

what am saying is that in ur code
$cv=$_POST[‘cv’];
//variables access
$cv_route = $_FILES[‘cv’][‘name’];
//but in this code here you used $cv instead of $cv_route, when cv_route is the filename
$query=“INSERT INTO applicant(vcode,fname,lname,email,mobile,phone,address,address2,city,state,zip,country,countryborn,stateoforigin,dob,sex, empstatus, currsalary, skills, cv) VALUES (’$vcode’,’$fname’,’$lname’,’$email’,’$mobile’,’$phone’,’$address’,’$address2’,’$city’,’$state’, ‘$zip’,’$country’,’$countryborn’,’$stateoforigin’,’$dob’,’$sex’,’$empstatus’, ‘$currsalary’, ‘$skills’, ‘$cv</look here>’)”;

before saving to the database comment the saving aspect and just print out your sql statement.

@dojoVader So what do u suggest I do? Happy New Year Bro

pls am designing an online project supervision and management system and i want someone to help me with the code for uploading and downloading ms doc from database