bugzy Posted July 17, 2012 Share Posted July 17, 2012 This is my form <form method="post" name="add_item" enctype="multipart/form-data" action="add_item.php"> <input name="uploaded_image" type="file" /> <input type="submit" value="Add Item" name="submit" size="30" /> my php code <?php if(isset($_post['submit'])) { $target_path = "images_item/"; if (!($_FILES["uploaded_image"]["type"] == "image/gif" || $_FILES["uploaded_image"]["type"] == "image/jpeg" || $_FILES["uploaded_image"]["type"] == "image/png") && $_FILES["uploaded_image"]["error"] != "4") { //invalid file format } else { move_uploaded_file($_FILES["uploaded_image"]["tmp_name"], $target_path); } } ?> The folder images_item is on the root but the code is not working as images that have been uploaded are nowhere to be found on images_item folder. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/265795-move_uploaded_file-is-not-working/ Share on other sites More sharing options...
requinix Posted July 17, 2012 Share Posted July 17, 2012 1. Uppercase $_POST. Variables are case-sensitive. 2. $target_path has to include the filename. 3. $_FILES[...]["type"] cannot be trusted. Use getimagesize to determine if the file is an image. 4. Your logic is a bit wrong. 5. You also need to check for other error codes. At the very least do an ==UPLOAD_ERR_OK. 6. Make sure your filename is safe. Has the right extension according to the type of image. Again, can't trust the $_FILES[...]["name"] to have the right extension - you need to set it yourself. Quote Link to comment https://forums.phpfreaks.com/topic/265795-move_uploaded_file-is-not-working/#findComment-1362041 Share on other sites More sharing options...
alexmark Posted July 17, 2012 Share Posted July 17, 2012 Hi There are several realy good tutorials on uploading images. I would check out http://codewall.blogspot.com/2011/03/upload-image-in-folder-in-php-step-by.html The prob with your form is that if it worked anyone could upload a diffrent file other than an image which leaves you open to problems with attacks from out side users. You need to make sure you can varify that the file been uploaded is indeed an image. Quote Link to comment https://forums.phpfreaks.com/topic/265795-move_uploaded_file-is-not-working/#findComment-1362060 Share on other sites More sharing options...
bugzy Posted July 17, 2012 Author Share Posted July 17, 2012 I finally get it working. The problem is the path.. Just a simple question guys.. right now I'm using php function "copy" , is it the same as "move_uploaded_file" ? Is it fine to use it? or will I'm going to have a problem latter on? Which do guys prefer? Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/265795-move_uploaded_file-is-not-working/#findComment-1362217 Share on other sites More sharing options...
requinix Posted July 17, 2012 Share Posted July 17, 2012 copy move_uploaded_file move_uploaded_file() is safer and actually moves the file. Quote Link to comment https://forums.phpfreaks.com/topic/265795-move_uploaded_file-is-not-working/#findComment-1362305 Share on other sites More sharing options...
bugzy Posted July 17, 2012 Author Share Posted July 17, 2012 Thanks requinix Quote Link to comment https://forums.phpfreaks.com/topic/265795-move_uploaded_file-is-not-working/#findComment-1362310 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.