PHP upload file to server and record name in database
Posted: Thu Feb 12, 2009 5:32 pm
Hello,
I have created a php upload form (with some help from others) that successfully uploads a pdf document to the server in a specific folder. I am also trying to have the file name updated in a mySQL database table so a dynamic link, linking to the newest file, will work correctly allowing the user to download the new file. I have searched and searched for a solution, and tried editing the code here and there, but I'm obviously not getting it. If anyone has time to look at my code and possibly help in any way it would be MUCH MUCH appreciated.
Thanks in advance!
here is the code:
HTML code for the form-
<form enctype="multipart/form-data" action="add.php" method="POST">
<p align="center">Please choose a file to upload:
<input name="doc" type="file" id="doc">
<br>
<input type="submit" value="Update File">
</p>
</form>
PHP code from add.php - (I'm assuming the problem lies in the area that "Writes the information to the database")
<?php
//This is the directory where images will be saved
$target = "../../docs/";
$target = $target . basename( $_FILES['doc']['name']);
//This gets all the other information from the form
$doc=($_FILES['doc']['name']);
// Connects to your Database
$hostname_shockhillDB = "localhost";
$database_shockhillDB = "dbname";
$username_shockhillDB = "usernamer";
$password_shockhillDB = "password";
$shockhillDB = mysql_pconnect($hostname_shockhillDB, $username_shockhillDB, $password_shockhillDB) or trigger_error(mysql_error(),E_USER_ERROR);
//Writes the information to the database
mysql_query("INSERT INTO 'd_file' VALUES ('$doc')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['doc']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
I have created a php upload form (with some help from others) that successfully uploads a pdf document to the server in a specific folder. I am also trying to have the file name updated in a mySQL database table so a dynamic link, linking to the newest file, will work correctly allowing the user to download the new file. I have searched and searched for a solution, and tried editing the code here and there, but I'm obviously not getting it. If anyone has time to look at my code and possibly help in any way it would be MUCH MUCH appreciated.
Thanks in advance!
here is the code:
HTML code for the form-
<form enctype="multipart/form-data" action="add.php" method="POST">
<p align="center">Please choose a file to upload:
<input name="doc" type="file" id="doc">
<br>
<input type="submit" value="Update File">
</p>
</form>
PHP code from add.php - (I'm assuming the problem lies in the area that "Writes the information to the database")
<?php
//This is the directory where images will be saved
$target = "../../docs/";
$target = $target . basename( $_FILES['doc']['name']);
//This gets all the other information from the form
$doc=($_FILES['doc']['name']);
// Connects to your Database
$hostname_shockhillDB = "localhost";
$database_shockhillDB = "dbname";
$username_shockhillDB = "usernamer";
$password_shockhillDB = "password";
$shockhillDB = mysql_pconnect($hostname_shockhillDB, $username_shockhillDB, $password_shockhillDB) or trigger_error(mysql_error(),E_USER_ERROR);
//Writes the information to the database
mysql_query("INSERT INTO 'd_file' VALUES ('$doc')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['doc']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>