Pacopag Posted August 4, 2011 Share Posted August 4, 2011 Hi. I have a site where users can upload image files. I currently accept only .jpg images. The problem is that some .jpg files just DON'T upload, and my users are becoming discouraged. However, if I take a .jpg file that refuses to be uploaded, and I just re-save it using Paint (i.e. open with Paint and just "Save As" a jpeg), then the re-saved file uploads no problem. Here's how I'm doing the upload. The html form part <form enctype="multipart/form-data" action="uploadpicture.php" target="some_target" name="someForm" id="someForm" method="post"> <label for="somefile">Filename:</label><input type="file" name="somefile" id="somefile" onchange="document.someForm.submit();" /> </form> The php part (i.e. in uploadpicture.php) include('SimpleImage.php'); if (($_FILES["somefile"]["type"] == "image/jpeg") && ($_FILES["somefile"]["size"] < 50000000)) { if ($_FILES["somefile"]["error"] > 0) { echo "Return Code: " . $_FILES["somefile"]["error"] . "<br />"; } else { $image = new SimpleImage(); $largeimage = new SimpleImage(); $image->load($_FILES["somefile"]["tmp_name"]); $largeimage->load($_FILES["somefile"]["tmp_name"]); $w = $image->getWidth(); $h = $image->getHeight(); if ($w > $h) { $aspect = 'w'; $largeimage->resizeToWidth(798); $image->resizeToWidth(225); } else { $aspect = 'h'; $largeimage->resizeToHeight(798); $image->resizeToHeight(225); } $image->save('newuploads/small.jpg',IMAGETYPE_JPEG,75,null); $largeimage->save('newuploads/large.jpg',IMAGETYPE_JPEG,75,null); } } The SimpleImage.php file can be found at http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/ Can someone identify why I might be having this problem? Or maybe give me another way to upload and resize images that works well.? Quote Link to comment https://forums.phpfreaks.com/topic/243830-image-upload-from-form/ Share on other sites More sharing options...
radiations3 Posted August 4, 2011 Share Posted August 4, 2011 Get some help from the following URL: http://www.codingmix.com/2011/01/php-gd-upload-image-downsize-compress.html Quote Link to comment https://forums.phpfreaks.com/topic/243830-image-upload-from-form/#findComment-1251938 Share on other sites More sharing options...
PFMaBiSmAd Posted August 4, 2011 Share Posted August 4, 2011 Your code (which appears to be based on the w3schools code) is not really performing any working error checking. Use the following code to find out why the upload is failing and give your visitors a more enjoyable experience (when validating user supplied data, you need to provide them with as much information as possible when the validation fails) - <?php include('SimpleImage.php'); // detect if a form was submitted if($_SERVER['REQUEST_METHOD'] == "POST"){ if(empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > ini_get('post_max_size')){ // the files array is empty and the size of the post data is greater than the max echo "The uploaded file size: ". number_format($_SERVER['CONTENT_LENGTH']) . " bytes, exceeded the maximum permitted post size: " . number_format(ini_get('post_max_size')) . " bytes."; } else { // test for uploaded errors if ($_FILES["somefile"]["error"] > 0){ // in a real application, you would want to display a helpful user messages instead of a code number echo "An upload error occurred, Code: " . $_FILES["somefile"]["error"]; } else { // no upload errors, perform your validation tests here... $types = array("image/jpeg"); if(!in_array($_FILES["somefile"]["type"],$types)){ echo "The file type: {$_FILES["somefile"]["type"]} is not a permitted type. Only ". implode(',',$types)." are allowed."; } else { $max_size = 50000000; if($_FILES["somefile"]["size"] >= $max_size){ echo "The file size: ". number_format($_FILES["somefile"]["size"]) . " bytes, is greater than the permitted size: " . number_format($max_size) ." bytes."; } else { // an image was uploaded correctly, without any errors, of the permitted type and file size, process the file here... $image = new SimpleImage(); $largeimage = new SimpleImage(); $image->load($_FILES["somefile"]["tmp_name"]); $largeimage->load($_FILES["somefile"]["tmp_name"]); $w = $image->getWidth(); $h = $image->getHeight(); if ($w > $h) { $aspect = 'w'; $largeimage->resizeToWidth(798); $image->resizeToWidth(225); } else { $aspect = 'h'; $largeimage->resizeToHeight(798); $image->resizeToHeight(225); } $image->save('newuploads/small.jpg',IMAGETYPE_JPEG,75,null); $largeimage->save('newuploads/large.jpg',IMAGETYPE_JPEG,75,null); } } } } } ?> The most likely problem is that different browsers (and different versions of the same browser) send different type information for the same file. If so in your case, you need to add an array entry to the $types array in the above code to match the actual type information that is being sent by the offending browser. Quote Link to comment https://forums.phpfreaks.com/topic/243830-image-upload-from-form/#findComment-1251971 Share on other sites More sharing options...
Pacopag Posted August 4, 2011 Author Share Posted August 4, 2011 Thank you extremely much for the detail of your example. I really appreciate it. And you taught me the value of rigorous error checking. It turns out my problem is that the files are just too big. I learned this from the output of $_FILES["somefile"]["error"], which gave error code of 1. Man I feel like a dolt now. Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/243830-image-upload-from-form/#findComment-1252238 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Stay away from w3schools. Quote Link to comment https://forums.phpfreaks.com/topic/243830-image-upload-from-form/#findComment-1252249 Share on other sites More sharing options...
Pacopag Posted August 5, 2011 Author Share Posted August 5, 2011 Yeah. I'm getting that feeling. I think that w3schools may be a good starting point, but you really gotta dig much deeper than what they give you to do things "right". Thanks for the advice. Quote Link to comment https://forums.phpfreaks.com/topic/243830-image-upload-from-form/#findComment-1252250 Share on other sites More sharing options...
PFMaBiSmAd Posted August 5, 2011 Share Posted August 5, 2011 To allow larger files to be uploaded, you can typically set the upload_max_filesize and post_max_size on your web hosting, either in a local php.ini (when php is running as a CGI application) or in a .htaccess file (when php is running as an Apache Module.) Quote Link to comment https://forums.phpfreaks.com/topic/243830-image-upload-from-form/#findComment-1252251 Share on other sites More sharing options...
Pacopag Posted August 5, 2011 Author Share Posted August 5, 2011 Right on! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/243830-image-upload-from-form/#findComment-1252260 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.