homepage Welcome to WebmasterWorld Guest from 54.205.193.39
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Small problem with php/mysql upload script
ballistix




msg:1257360
 9:54 pm on Jun 29, 2004 (gmt 0)

Hi all

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

 

m_shroom




msg:1257361
 4:32 am on Jun 30, 2004 (gmt 0)

You ask to much.

With all the code you shown us you have no mysql_query();

I don't think anyone here wants to rewrite the book.

Read the book get, get it close, then ask for help.

ballistix




msg:1257362
 1:13 pm on Jun 30, 2004 (gmt 0)

Sorry, in hindsight, I suppose I should have included the code that I am using. Here it is:

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!

coopster




msg:1257363
 2:02 pm on Jun 30, 2004 (gmt 0)

Welcome to WebmasterWorld, ballistix!

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')";

Birdman




msg:1257364
 2:14 pm on Jun 30, 2004 (gmt 0)

Here is a shorter version that does the check for upload and saves it at once. I also added the missing mysql_query() statement and a description for the pic. Simply add the extra form field and db field for the description.

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.";
}
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved