Marvin_G Posted January 28, 2013 Share Posted January 28, 2013 hye guys,i have some problem regarding this command -> move_uploaded_file(). it seems that the image uploaded to server (tmp folder) can't be moved to upload/. below is my command: <script type="text/javascript"> function show_alert() { var msg = "Invalid File!"; alert(msg); window.history.back(); } </script> <script type="text/javascript"> function show_alert2() { var msg2 = "File already exist!"; alert(msg2); window.history.back(); } </script> <script type="text/javascript"> function show_alert3() { var msg3 = "File Sucessfully moved!"; alert(msg3); // window.history.back(); } </script> <script type="text/javascript"> function show_alert4() { var msg4 = "File NOT Sucessfully moved!"; alert(msg4); // window.history.back(); } </script> <?php $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] > 10) && 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"] . "<br>"; if (file_exists( "upload/" . $_FILES["file"]["name"])) { echo '<script type="text/javascript"> show_alert2(); </script>'; } else { $uploaded_dir = "upload/"; $filename = $_FILES["file"]["name"]; $path = $uploaded_dir . $filename; move_uploaded_file($_FILES["file"]["tmp_name"], $path); // move_uploaded_file( $_FILES["file"]["tmp_name"] , "upload/" . $_FILES["file"]["name"] ); if ($moved) //check if the file were moved or not { echo '<script type="text/javascript"> show_alert3(); </script>'; } else { echo '<script type="text/javascript"> show_alert4(); </script>'; } // echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo '<script type="text/javascript"> show_alert(); </script>'; } ?> here is the html command: <html> <body> <form action="complete_upload_file_w_restriction_w_javascript_alert.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> Please help me with this guys. thanks alot. Quote Link to comment https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/ Share on other sites More sharing options...
requinix Posted January 28, 2013 Share Posted January 28, 2013 it seems that the image uploaded to server (tmp folder) can't be moved to upload/. Why do you say that? Are you getting any error messages? If not then make sure your php.ini has display_errors = on error_reporting = -1 (restart the server if you have to change it), and try again. Quote Link to comment https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/#findComment-1408812 Share on other sites More sharing options...
Marvin_G Posted January 29, 2013 Author Share Posted January 29, 2013 Why do you say that? Are you getting any error messages? If not then make sure your php.ini has display_errors = on error_reporting = -1 (restart the server if you have to change it), and try again. if u look in my code, i put if ($moved) to see whether the file from the tmp folder moved to "upload" folder. but it always return false which means not successfullt moved. how? Quote Link to comment https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/#findComment-1408891 Share on other sites More sharing options...
Love2c0de Posted January 29, 2013 Share Posted January 29, 2013 You've not actually saved the return value of move_uploaded_file(). Change this: move_uploaded_file($_FILES["file"]["tmp_name"], $path); To this: $moved = move_uploaded_file($_FILES["file"]["tmp_name"], $path); You need to do what requinix said because had you turned on error reporting, it probably would have give you an error of 'Undefined variable $moved'. Hope this helps, Regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/#findComment-1408892 Share on other sites More sharing options...
Marvin_G Posted January 29, 2013 Author Share Posted January 29, 2013 if u look in my code, i put if ($moved) to see whether the file from the tmp folder moved to "upload" folder. but it always return false which means not successfullt moved. how? where should i put it? in my .php file? Quote Link to comment https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/#findComment-1408907 Share on other sites More sharing options...
Marvin_G Posted January 29, 2013 Author Share Posted January 29, 2013 You've not actually saved the return value of move_uploaded_file(). Change this: move_uploaded_file($_FILES["file"]["tmp_name"], $path); To this: $moved = move_uploaded_file($_FILES["file"]["tmp_name"], $path); You need to do what requinix said because had you turned on error reporting, it probably would have give you an error of 'Undefined variable $moved'. Hope this helps, Regards, L2c. ok thank you so much. where can i check the uploaded data? can i download it back? Quote Link to comment https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/#findComment-1408909 Share on other sites More sharing options...
Marvin_G Posted January 29, 2013 Author Share Posted January 29, 2013 i already succesful move the file to "upload/". the current problem is where does the"upload/" stored? i'm using /var/www directory but i can't find the "upload" folder. i need to save file to the server and able to download it back in any device. Quote Link to comment https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/#findComment-1408913 Share on other sites More sharing options...
Marvin_G Posted January 29, 2013 Author Share Posted January 29, 2013 i have another problem. when im using http://localhost/~myname/..i can't execute .php file. it keep telling me to use editor to open. why is that? Quote Link to comment https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/#findComment-1408917 Share on other sites More sharing options...
Marvin_G Posted January 29, 2013 Author Share Posted January 29, 2013 if u look in my code, i put if ($moved) to see whether the file from the tmp folder moved to "upload" folder. but it always return false which means not successfullt moved. how? i got this in my php.ini ; display_errors ; Default Value: On ; Development Value: On ; Production Value: Off ; error_reporting ; Default Value: E_ALL & ~E_NOTICE ; Development Value: E_ALL | E_STRICT ; Production Value: E_ALL & ~E_DEPRECATED do i need to change anything? Quote Link to comment https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/#findComment-1408927 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.