lou22 Posted January 25, 2012 Share Posted January 25, 2012 Hi, I've managed to get script 10.3 upload_image.php working in as far as the form displays, I can select a file, submit it, and get a message saying that the file has uploaded. But I can't find the file in the uploads folder. Not sure what is going wrong. I'm using Vista home basic,Dreamweaver cs4, xampp v3.02 with Apache 2.0 Handler. phpinfo gives the configuration path for php.ini as C:\Windows and Loaded configuration file as D:xampp\php\php.ini. The upload_tmp_dr is D:\xampp\tmp. according to phpinfo and d:\xampp\php\php.ini. The properties box for d:\xampp\tmp dir doesn't specifically say whether it's private or not but I successfully saved a copy of the upload_image.php file there from Dreamweaver so I assume not, although I am logged in as administrator. My code is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Upload an Image</title> <style type="text/css" title="text/css" media="all"> .error{ font-weight: bold; color:#c00 } </style> </head> <body> <?php #Script 10.3 upload_image.php //check if the form has been submitted: if(isset($_POST['submitted'])){ //check for an upload file: if(isset($_FILES['upload'])){ //Validate the type. Should be JPEG or PNG: $allowed = array('image/pjpeg','image/jpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png'); if(in_array($_FILES['upload']['type'],$allowed)){ //move the file over if(move_uploaded_file($_FILES['upload']['tmp_name'],"../../../tmp{$_FILES['upload']['name']}")){//php.info says default upload file is xampp/tmp echo '<p><em>The file has been uploaded!</em></p>'; }//End of move IF }else{//invalid type echo '<p class=""error">Please upload a JPEG or PNG image.</p>'; } }//End of isset($_FILES['upload']) IF //check for an error: if($_FILES['upload']['error']>0){ echo '<p class=""error">The file could not be uploaded because: <strong>'; switch($_FILES['upload']['error']){ case 1: print 'The file exceeds the upload_max_filesize setting in php.ini.'; 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 file was uploaded.'; break; case 6: print 'No temporary folder was available.'; break; case 7: print 'Unable to write to the disk.'; break; case 8: print 'File upload stopped.'; break; default: print 'A system error occurred.'; break; }//end of switch print '</strong></p>'; }// End of error IF //delete the file if it still exists: if(file_exists($_FILES['upload']['tmp_name'] && is_file($_FILES['upload']['tmp_name']))){ unlink($_FILES['upload']['tmp_name']); } }//End of the submitted conditional ?> <div align="center"> <form enctype="multipart/form-data" action="upload_image.php" method="post" style="width:400px"> <input type="hidden" name="MAX_FILE_SIZE" value="524288" > <fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend> <p><b><input type="file" name="upload"/></b></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit"/></div> <input type="hidden" name="submitted" value="TRUE"/> </form> </div> </body> </html> .error{ font-weight: bold; color:#c00 } //check if the form has been submitted: if(isset($_POST['submitted'])){ //check for an upload file: if(isset($_FILES['upload'])){ //Validate the type. Should be JPEG or PNG: $allowed = array('image/pjpeg','image/jpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png'); if(in_array($_FILES['upload']['type'],$allowed)){ //move the file over if(move_uploaded_file($_FILES['upload']['tmp_name'],"../../../tmp{$_FILES['upload']['name']}")){//php.info says default upload file is xampp/tmp echo 'The file has been uploaded! '; }//End of move IF }else{//invalid type echo 'Please upload a JPEG or PNG image. '; } }//End of isset($_FILES['upload']) IF //check for an error: if($_FILES['upload']['error']>0){ echo 'The file could not be uploaded because: '; switch($_FILES['upload']['error']){ case 1: print 'The file exceeds the upload_max_filesize setting in php.ini.'; 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 file was uploaded.'; break; case 6: print 'No temporary folder was available.'; break; case 7: print 'Unable to write to the disk.'; break; case 8: print 'File upload stopped.'; break; default: print 'A system error occurred.'; break; }//end of switch print ' '; }// End of error IF //delete the file if it still exists: if(file_exists($_FILES['upload']['tmp_name'] && is_file($_FILES['upload']['tmp_name']))){ unlink($_FILES['upload']['tmp_name']); } }//End of the submitted conditional ?> Select a JPEG or PNG image of 512KB or smaller to be uploaded: I'd be grateful for any help</p> Link to comment Share on other sites More sharing options...
lou22 Posted January 25, 2012 Author Share Posted January 25, 2012 Sorry, I'm having trouble editing the post. The code is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Upload an Image</title> <style type="text/css" title="text/css" media="all"> .error{ font-weight: bold; color:#c00 } </style> </head> <body> <?php #Script 10.3 upload_image.php //check if the form has been submitted: if(isset($_POST['submitted'])){ //check for an upload file: if(isset($_FILES['upload'])){ //Validate the type. Should be JPEG or PNG: $allowed = array('image/pjpeg','image/jpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png'); if(in_array($_FILES['upload']['type'],$allowed)){ //move the file over if(move_uploaded_file($_FILES['upload']['tmp_name'],"../../../tmp{$_FILES['upload']['name']}")){//php.info says default upload file is xampp/tmp echo '<p><em>The file has been uploaded!</em></p>'; }//End of move IF }else{//invalid type echo '<p class=""error">Please upload a JPEG or PNG image.</p>'; } }//End of isset($_FILES['upload']) IF //check for an error: if($_FILES['upload']['error']>0){ echo '<p class=""error">The file could not be uploaded because: <strong>'; switch($_FILES['upload']['error']){ case 1: print 'The file exceeds the upload_max_filesize setting in php.ini.'; 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 file was uploaded.'; break; case 6: print 'No temporary folder was available.'; break; case 7: print 'Unable to write to the disk.'; break; case 8: print 'File upload stopped.'; break; default: print 'A system error occurred.'; break; }//end of switch print '</strong></p>'; }// End of error IF //delete the file if it still exists: if(file_exists($_FILES['upload']['tmp_name'] && is_file($_FILES['upload']['tmp_name']))){ unlink($_FILES['upload']['tmp_name']); } }//End of the submitted conditional ?> <div align="center"> <form enctype="multipart/form-data" action="upload_image.php" method="post" style="width:400px"> <input type="hidden" name="MAX_FILE_SIZE" value="524288" > <fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend> <p><b><input type="file" name="upload"/></b></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit"/></div> <input type="hidden" name="submitted" value="TRUE"/> </form> </div> </body> </html> Link to comment Share on other sites More sharing options...
Larry Posted January 25, 2012 Share Posted January 25, 2012 D:\xampp\tmp is your TEMPORARY directory, where the file will initially be stored. You need to also create an uploads folder, which will be the final resting place for the uploaded file. The server will automatically clear files out of the temporary directory if they aren't moved from it. Link to comment Share on other sites More sharing options...
lou22 Posted January 25, 2012 Author Share Posted January 25, 2012 Thanks Larry - I love the book by the way! The files had uploaded - I found them in d:\xampp\ instead of d:\xampp\tmp - and all the file names were prefixed with 'tmp' - I'd missed out the last backslash in the file path. I've now successfully uploaded a jpeg to d:\xampp\tmp. I appreciate that d:\xampp\tmp is temporary but I just wanted to get it working. I've now changed the path in the move_upload_file to d:\xampp\upload\ and successfully tested that too. Thanks for your help. 1 Link to comment Share on other sites More sharing options...
Larry Posted January 25, 2012 Share Posted January 25, 2012 Excellent. Thanks for sharing what you discovered and for the nice words on the book! Link to comment Share on other sites More sharing options...
Recommended Posts