Jump to content

Recommended Posts

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.?

Link to comment
https://forums.phpfreaks.com/topic/243830-image-upload-from-form/
Share on other sites

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.

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.

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.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.