Page 1 of 1

PHP upload file to server and record name in database

Posted: Thu Feb 12, 2009 5:32 pm
by WilsonLass
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.";
}
?>

Re: PHP upload file to server and record name in database

Posted: Thu Feb 12, 2009 5:45 pm
by Benjamin
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]

Re: PHP upload file to server and record name in database

Posted: Fri Feb 13, 2009 10:24 am
by WilsonLass
Hello,

I apologize for not posting my code correctly. I still can't seem to figure it out...
so here is a link to an image of the html form code: http://www.wilsonlass.com/images/htmlFormCode.jpg

and this is a link to the php code: http://www.wilsonlass.com/images/phpCode.jpg

once again, I am trying to upload a file to the server (which works ok) and update the file name in the database (which is not working).
I am trying to update the file name in a table called "docs" and the field name of "d_file"

I appreciate any help!