DarkPrince2005 Posted May 12, 2008 Share Posted May 12, 2008 Hi guys & gals I'm trying to upload an image to a folder, and at the same time store that path in the database, but i'm having trouble storing the file name.. Please help. Upload form <html> <body><form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form></body> </html> Upload script <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/bmp") || ($_FILES["file"]["type"] == "image/gif")) && ($_FILES["file"]["size"] < 200000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; mysql_connect("localhost","root",""); mysql_select_db("pix"); mysql_query("insert into pix values('',\"upload/$_FILES[file][name]\")"); if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> the above code though works but it stores the path as 'upload/Array[name]' Link to comment https://forums.phpfreaks.com/topic/105323-storing-uploaded-image-path-in-database/ Share on other sites More sharing options...
flyhoney Posted May 12, 2008 Share Posted May 12, 2008 Try this: <?php $filename = $_FILES["file"]["name"]; mysql_query("insert into pix values('','upload/$filename')"); ?> Link to comment https://forums.phpfreaks.com/topic/105323-storing-uploaded-image-path-in-database/#findComment-539360 Share on other sites More sharing options...
craygo Posted May 12, 2008 Share Posted May 12, 2008 Just for the future, if you want to include array vars in an sql statement, you either have to come out of the quotes or enclose the value in brackets mysql_query("insert into pix values('','upload/".$_FILES['file']['name']."')"); or mysql_query("insert into pix values('','upload/{$_FILES['file']['name']}')"); Ray Link to comment https://forums.phpfreaks.com/topic/105323-storing-uploaded-image-path-in-database/#findComment-539377 Share on other sites More sharing options...
DarkPrince2005 Posted May 12, 2008 Author Share Posted May 12, 2008 Ha-ha... thank you. But can you maybe also advise me on how i would change the upload script to, if the filename exists that it should add a 1 or something to the filename. I would guess that'll involve fread(), fopen() or something like that, which i have absolutely no clue about. Link to comment https://forums.phpfreaks.com/topic/105323-storing-uploaded-image-path-in-database/#findComment-539397 Share on other sites More sharing options...
flyhoney Posted May 12, 2008 Share Posted May 12, 2008 Instead of looking for the file, to see if there is a duplicate, why dont you just query the database and see if the filename already exists. Link to comment https://forums.phpfreaks.com/topic/105323-storing-uploaded-image-path-in-database/#findComment-539400 Share on other sites More sharing options...
DarkPrince2005 Posted May 12, 2008 Author Share Posted May 12, 2008 Is there a difference in how it is done? Link to comment https://forums.phpfreaks.com/topic/105323-storing-uploaded-image-path-in-database/#findComment-539412 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.