shanna Posted December 7, 2016 Share Posted December 7, 2016 !doctype html> <html lang="en"> <head> <meta charset="utf-8"> </head> <body> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST'){ if (move_uploaded_file($_FILES['the_file']['tmp_name'],"C:\uploads\{$_FILES['the_file']['name']}")) { print '<p>your file has been uploaded.</p>'; }else { print '<p style="color:red;">Your file couldnot be uploaded because :'; switch ($_FILES['the_file']['error']) { case 1: print 'the file excceeds the upload_max-file size setting in php.oni'; break; case 2: print 'The file exceeds the max-FILE_SIZE SETTING IN THE HTML FORM'; break; case 3: print 'The file was only partially uploaded'; break; case 4: print 'No fil was uploaded'; break; case 6: print 'The temporary folder does not exixts,'; break; default: print 'something unforeseen happend'; break; } print '.</p>'; } } ?> <form action="upload_file.php" enctype="multipart/form-data" method="post"> <p> Upload a file using this form:</p> <input type="hidden" name="MAX_FILE_SIZE" value="300000"> <p><input type="file" name="the_file"></p> <p><input type="submit" name="submit" value="upload this file"></p> </form> </body> </html> when i try to run this i get this error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\PHPexamples\chapter11\upload_file.php on line 10 not understanding move_uploaded_file() function here. if i am right, i should be able to upload any file from PC and load it to uploads folder? its bit confusing and not clear ,,page 318-320 .. i didnt even get html form to upload any file. i had saved jpg file in some different directory and wanted to upload that and move it to uploads folder.. please some one explain. thank you. Link to comment Share on other sites More sharing options...
shanna Posted December 7, 2016 Author Share Posted December 7, 2016 i changed my uploads folder to ..C:/uploads/{$_FILES... then i am getting error as: Warning: move_uploaded_file(..C:/uploads/sun-flower.jpg): failed to open stream: Invalid argument in C:\xampp\htdocs\PHPexamples\chapter11\upload_file.php on line 11Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php968F.tmp' to '..C:/uploads/sun-flower.jpg' in C:\xampp\htdocs\PHPexamples\chapter11\upload_file.php on line 11 Your file couldnot be uploaded because :something unforeseen happend. Upload a file using this form: guess i am making simple mistake in making /creating folder or addressing it...which i am not able to get it ! Link to comment Share on other sites More sharing options...
Larry Posted December 8, 2016 Share Posted December 8, 2016 .. is a computer way of going up a directory. This begins a relative path. C:/ is a Windows way of referring to the root directory of the C harddrive. This begins an absolute path. You cannot use both at the same time. Depending upon where your uploads folder is, you likely want to use either ../uploads or C:/xampp/htdocs/PHPexamples/chapter11/uploads. Link to comment Share on other sites More sharing options...
possowski Posted September 24, 2018 Share Posted September 24, 2018 I am also having some issue with the directory path and don't want to put it in the same folder as the upload_file.php file. I have attached a snippet of my directory and here is the corresponding code: if (move_uploaded_file ($_FILES['the_file']['tmp_name'], "c:/xampp/uploads/ {$_FILES['the_file']['name']}")) { print '<p>Your file has been uploaded.</p>'; } else { // problem occurs My form is loading loading and seems to be functioning properly. this is my error message: Warning: move_uploaded_file(c:/xampp/uploads/ Freedom PE form.docx): failed to open stream: No such file or directory in C:\xampp\htdocs\FilesAndDirectoriesCh11\upload_file.php on line 19Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpE046.tmp' to 'c:/xampp/uploads/ Freedom PE form.docx' in C:\xampp\htdocs\FilesAndDirectoriesCh11\upload_file.php on line 19 Your file could not be uploaded because: Something unforeseen happened. Link to comment Share on other sites More sharing options...
Larry Posted September 24, 2018 Share Posted September 24, 2018 Hmm...this isn't obvious to me. A guess is that "c" vs. "C" may be a problem, but that's just a blind guess. I have no idea if that's a problem for Windows or not. My second guess is that initial space in the file name is causing a problem. Make sure that move_uploaded_file() code is on one line. Link to comment Share on other sites More sharing options...
possowski Posted September 24, 2018 Share Posted September 24, 2018 Larry, I changed to "c" to "C" and put the code on a single line and it works, thanks. Link to comment Share on other sites More sharing options...
possowski Posted September 24, 2018 Share Posted September 24, 2018 I am using Atom and had it on soft toggle word wrap and that made it look like the move_uploaded_file() code line was on a single line, but it wasn't. Link to comment Share on other sites More sharing options...
Larry Posted September 24, 2018 Share Posted September 24, 2018 Cool. Glad it's working now and thanks for letting us know! Link to comment Share on other sites More sharing options...
Recommended Posts