Forum Moderators: coopster
I have been trying for a couple of days to code a php/mysql upload. I have really been struggling to get it to work, until I found this post in the archives:
How about uploading your images to a pre-defined folder and just store the name of the image in your database?
The upload form:
<form enctype="multipart/form-data" action="upload.php" method="post" name="upload_file">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000">
<input name="userfile" type="file">
<input type="submit" value="upload" name="file_uploaded">
</form>upload.php:
<?php
// file upload - In PHP 4.1.0 or later, $_FILES should be used instead of $HTTP_POST_FILES.if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
//upload
$file_realname = $_FILES['userfile']['name'];//PUT IN FILDER
copy($_FILES['userfile']['tmp_name'], "path/to/folder/goes/here/$file_realname");
}
else
{
echo "<b><font color=red>No file uploaded.</font></b><BR>No file available or file too big to upload.";
}
?>$file_realname can be saved in database.
Be Positive
So now the upload works perfectly except for one thing, the filename is not being stored in the DB. The person mentioned that $file_realname can be saved to the database. I don't really know how to do this, please could somebody help me with the code that I would need to add and where I have to add it into the php.
The next thing is, if I add a description field to my DB and form, what code would I have to add to the php?
And lastly, how do I get a field in my from my DB to display as a link. I can retrieve fields from DB's without a hassle, I'm just not sure how to make them links
Sorry, I know this is a lengthy post and that I have asked quite a few questions, but I am desperately trying to learn this and would greatly appreciate any help you guys can give me.
Thanks in advance,
Ballistix
The upload form(index.php):
<form enctype="multipart/form-data" action="upload.php" method="post" name="upload_file">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000">
<input name="userfile" type="file">
<input type="submit" value="upload" name="file_uploaded">
</form>The upload.php page:
<?php
//FILE UPLOAD
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{MYSQL_CONNECT("localhost","root","password");
mysql_select_db("upload");//upload
$file_realname = $_FILES['userfile']['name'];//INSERT FILE NAME IN DB
$sql="insert into picpath (data_file) values ($file_realname)";//PUT UPLOADED FILE IN FOLDER
copy($_FILES['userfile']['tmp_name'], "D:/Inetpub/wwwroot/upload/images/$file_realname");}
else
{
echo "<b><font color=red>No file uploaded.</font></b><BR>No file available or file too big to upload.";
}
?>
So here is the problem, I thought the query, $sql="insert into picpath (data_file) values ($file_realname)"; would enter the filename into the DB field, but it's not. So the file is being uploaded to the dir, but the name is not being saved in the db.
Please could you let me know where I am making the mistake with my sql statement.
Thanks!
Well, assuming you are getting connected to the database OK, about the only thing I see missing here would be quotation marks around your filename variable as I would assume it is going to be of column type character/text. I'm not sure if your server has Magic Quotes [webmasterworld.com] on or not, so you may need to addslashes [php.net] to that variable first as well...
//INSERT FILE NAME IN DB
$sql="insert into picpath (data_file) values ('$file_realname')";
You should remove the 'or die(mysql_error())' party once it's working well.
<?php
if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $_FILES["userfile"]["name"]))
{
mysql_connect("localhost","root","password");
mysql_select_db("upload");
$sql="insert into picpath (data_file, descrip) values ('".$_FILES["userfile"]["name"]."', ".$_POST["descrip"].")";
mysql_query($sql)or die(mysql_error())
}
else
{
echo "<b><font color=red>No file uploaded.</font></b><BR>No file available or file too big to upload.";
}
?>