elviapw Posted February 14, 2009 Share Posted February 14, 2009 Hello! I'm trying to create a page for users to upload text and images to a MYSQL database. However, I I'd like to store only the image information in the database, but move the image itself in a folder in an image directory on the website. I hope that makes sense so far. The code I have so far is intended to store the text information in the database and upload the image directly to a folder on the website called /images. I'm having trouble getting it to work. First, here is the HTML form for the user to input the data: <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>post</title> </head> <body> POST<br/><br/> <form action="posted.php" method="post" enctype="multipart/form-data"> title:<br /> <input type="text" name="title" size="93"/> (optional)<br /> date: <br/> <input type="text" name="date" /><br/> post:<br /> <textarea rows="100" cols="80" name="post"></textarea><br /> pictures: <br/> <input type="file" name="picture" accept="image/jpeg"/> <input type="submit" name="Submit" value="do it"/><br /> </form> </body> </html> and here is the PHP file (posted.php) : <?php $target = "/images"; $target = $target . basename ($_FILES['picture']); $title = $_POST['title']; $post = $_POST['post']; $date = $_POST['date']; $picture = $_FILES['picture']; mysql_connect("--", "--", "--") or die(mysql_error()) ; mysql_select_db("dbname") or die(mysql_error()) ; mysql_query("INSERT INTO Posts (Number,Title,Content,Date,Picture) VALUES (null,'$title', '$post', '$date', '$picture')") ; if(move_uploaded_file $_FILES['picture'], $target) { echo "The file ". basename $_FILES['picture']. " has been uploaded, and your information has been added to the directory"; } else { echo "Sorry, there was a problem uploading your file."; } ?> The result is the message "I'm sorry, there was a problem uploading your file" I'm fairly sure the problem is with the $target or $_FILES variables. I know the connection to MYSQL works because I've gotten a different form to work that uploads only text to the database, but the images are posing a problem. I'm not sure I understand the basic concept for moving an image file to a remote directory. Could someone help me with the syntax? Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/ Share on other sites More sharing options...
RichardRotterdam Posted February 14, 2009 Share Posted February 14, 2009 have you checked the writing permission of the folder you're trying to upload the file to? Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762133 Share on other sites More sharing options...
bubbasheeko Posted February 14, 2009 Share Posted February 14, 2009 Try this if your permissions are fine: if(move_uploaded_file($_FILES['picture'], $target)) { echo "The file ". basename($_FILES['picture']). " has been uploaded, and your information has been added to the directory"; } else { echo "Sorry, there was a problem uploading your file."; That would explain why it would be failing the first check and jumping to the else. Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762148 Share on other sites More sharing options...
elviapw Posted February 14, 2009 Author Share Posted February 14, 2009 Where do I find out about permissions? How can I change them? Does that depend on my service provider? Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762162 Share on other sites More sharing options...
angelcool Posted February 14, 2009 Share Posted February 14, 2009 Check this out: http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx Good Luck! Angel Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762166 Share on other sites More sharing options...
bubbasheeko Posted February 14, 2009 Share Posted February 14, 2009 Depends on your FTP program. If you use say...Firefox and FireFTP...you just simply right click on the images folder and set permissions. Most cases in FTP programs...you would just right click on the file or folder and choose to set the permissions. You could get a way with 755..but if not you would need to set the permissions to 777. Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762167 Share on other sites More sharing options...
bubbasheeko Posted February 14, 2009 Share Posted February 14, 2009 angelcool - elvia is talking about the form information into the database...not the image itself. There is a form with information about the image that is being submitted to the database. The image is being stored in the images directory. Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762169 Share on other sites More sharing options...
angelcool Posted February 14, 2009 Share Posted February 14, 2009 Sorry, I only read the subject "PHP upload image to database" I should have kept on reading. Anyhow, its good reference for anyone. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762173 Share on other sites More sharing options...
bubbasheeko Posted February 14, 2009 Share Posted February 14, 2009 I have that one bookmarked as well, thanks for sharing Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762177 Share on other sites More sharing options...
elviapw Posted February 14, 2009 Author Share Posted February 14, 2009 thanks for the suggestions! i made sure that the images folder and subfolders were readable/writable/executable by anyone, and tried adding the missing sets of parentheses to the code as bubbasheeko suggested. BUT unfortunately still no luck... could it be a problem with the way i have my target folder formatted? here's the code with the new parentheses: <?php $target = "/images/posts"; $target = $target . basename ($_FILES['picture']); $title = $_POST['title']; $post = $_POST['post']; $date = $_POST['date']; $picture = $_FILES['picture']; mysql_connect("--", "--", "--", "--") or die(mysql_error()) ; mysql_select_db("dbname") or die(mysql_error()) ; mysql_query("INSERT INTO Posts (Number,Title,Content,Date,Picture) VALUES (null,'$title', '$post', '$date', '$picture')") ; if(move_uploaded_file ($_FILES['picture'], $target)) { echo "The file ". basename ($_FILES['picture']). " has been uploaded, and your information has been added to the directory"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762184 Share on other sites More sharing options...
angelcool Posted February 14, 2009 Share Posted February 14, 2009 Try: move_uploaded_file ($_FILES['picture']['tmp_name'], $target) as indicated here: http://www.php.net/manual/en/features.file-upload.post-method.php See if that works. Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762191 Share on other sites More sharing options...
angelcool Posted February 14, 2009 Share Posted February 14, 2009 I think there might also be a conflict with the $target variable in the move_uploaded_file() function, try: $target='/images/posts/'.$_FILES['userfile']['name']; move_uploaded_file ($_FILES['picture']['tmp_name'],$target) Also check you /tmp directory in your box, all uploaded files get temporarly store here; So after submitting a file (with your HTML form), monitor your /tmp directory and see if a file is created (with a weird name). :-) Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-762267 Share on other sites More sharing options...
angelcool Posted February 15, 2009 Share Posted February 15, 2009 I'll quote myselft, Also check you /tmp directory in your box, all uploaded files get temporarly store here; So after submitting a file (with your HTML form), monitor your /tmp directory and see if a file is created (with a weird name). This behavior occurs where PHP sessions are stored (session.save_path in php.ini); this does not seem the same with upload_tmp_dir, as I ran an upload script, file successfully uploaded but did not see the same behavior, perhaps someone can confirm this. I guess I got this two parameters mixed up when first posted this. Angel Quote Link to comment https://forums.phpfreaks.com/topic/145200-php-upload-image-to-database/#findComment-763009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.