justlukeyou Posted October 16, 2012 Share Posted October 16, 2012 Hi, I am trying to introduce an error upload script however I have one last error which I am struggling to fix. This is the error message. I had 3 other errors which I have managed to fix. I have even put in the full link however this doesn't seem to fix the problem. Any suggestions please? Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php5BdCin' to 'http://www.website.com/test/imagetest.gif' in /home/website.com/test/upload_file.php on line 79 <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"]; } ?> <?php $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; } ?> <?php $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts)) { 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 />"; if (file_exists("http://www.website.com/test/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "http://www.website.com/test/" . $_FILES["file"]["name"]); *******ERROR RELATED HERE******* echo "Stored in: " . "http://www.website.com/test/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269555-last-error-with-image-upload/ Share on other sites More sharing options...
DavidAM Posted October 16, 2012 Share Posted October 16, 2012 move_uploaded_file($_FILES["file"]["tmp_name"], "http://www.website.com/test/" . $_FILES["file"]["name"]); *******ERROR RELATED HERE******* You don't move_uploaded_file to your website URL; you move it to a filesytem directory. In this case, you might want to use $_SERVER['DOCUMENT_ROOT'] as the path. By the way, why do you have the same code repeated over and over? Quote Link to comment https://forums.phpfreaks.com/topic/269555-last-error-with-image-upload/#findComment-1385682 Share on other sites More sharing options...
requinix Posted October 16, 2012 Share Posted October 16, 2012 You can't copy files into a website. You have to copy them as the actual files they are. Figure out where the /test path is on your filesystem (spoiler: $_SERVER["DOCUMENT_ROOT"] . "/test") and copy the files into that. And you know you've got a block of code there repeated, right? And don't use the ["type"] to tell the file type - it cannot be trusted and might even be wrong in some (coughIE) browsers. Look at the extension first and, for images, use getimagesize to be extra sure. Quote Link to comment https://forums.phpfreaks.com/topic/269555-last-error-with-image-upload/#findComment-1385683 Share on other sites More sharing options...
justlukeyou Posted October 16, 2012 Author Share Posted October 16, 2012 Cheers dudes, my silly mistake. I was using this guide http://php.about.com/od/advancedphp/ss/php_file_upload_3.htm I have one last issue which does make sense. I have sent the destination as "/home/domain/test" so the image is entered into a test folder. However the image is being entered into "/home/domain/" the folder above the test folder. Can anyone advise how to place it into the actual test folder? echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("/home/domain/test" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "/home/domain/test" . $_FILES["file"]["name"]); echo "Stored in: " . "/home/domain/test" . $_FILES["file"]["name"]; } } Quote Link to comment https://forums.phpfreaks.com/topic/269555-last-error-with-image-upload/#findComment-1385685 Share on other sites More sharing options...
requinix Posted October 16, 2012 Share Posted October 16, 2012 Trailing slash in the path. Can haz one? Without it the filenames look like "/home/domain/testimage.jpg". Quote Link to comment https://forums.phpfreaks.com/topic/269555-last-error-with-image-upload/#findComment-1385687 Share on other sites More sharing options...
justlukeyou Posted October 16, 2012 Author Share Posted October 16, 2012 (edited) Thanks I shall try "/home/domain/test/" Many thanks. Edited October 16, 2012 by justlukeyou Quote Link to comment https://forums.phpfreaks.com/topic/269555-last-error-with-image-upload/#findComment-1385688 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.