2 Replies - 536 Views - Last Post: 05 July 2011 - 01:32 AM Rate Topic: -----

#1 scentauri  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 16-February 11

Larger images not displaying triggering error

Posted 05 July 2011 - 12:41 AM

I've modified a code to upload and rename an image file a bit in PHP; however, I'm running into a slight problem. Everything seems to be working fine, except for when I upload a very large image file. The code is set that if the image size is larger than 200Kb then $sizeError is set to true, which is a condition to prevent an add listing from occurring. However, when the image is very large it seems not to set the $sizeError to true, causing the listing to be added without triggering an error to tell users that the image is too large. I tested by adding the following code to the beginning to check what the file size is displayed as:

$size = filesize($_FILES['imgfile']['tmp_name']);

echo $size;



However, the code above for larger image sizes doesn't display the image size of the uploaded image.

The code for the image upload and rename in its entirety is below:

	define("MAX_SIZE","200");

	$imgfile = $_FILES['imgfile']['tmp_name'];
	$imgLink = NULL;

	if($imgfile){
		if(is_uploaded_file($imgfile)){
			$image = $_FILES["imgfile"]["name"];
			$uploadedfile = $_FILES['imgfile']['tmp_name'];

			if ($image){
				$filename = $_FILES['imgfile']['name'];
				$extension = pathinfo($filename, PATHINFO_EXTENSION);
				$extension = strtolower($extension);
				$size = filesize($_FILES['imgfile']['tmp_name']);
				if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
					$extError = true;
				}
				else if ($size > MAX_SIZE*1024){
					$sizeError = true;
				}
				else{	 
					if ($extension=="jpg" || $extension=="jpeg" ){
						$uploadedfile = $_FILES['imgfile']['tmp_name'];
						$src = imagecreatefromjpeg($uploadedfile);
					}
					else if ($extension=="png"){
						$uploadedfile = $_FILES['imgfile']['tmp_name'];
						$src = imagecreatefrompng($uploadedfile);
					}
					else{
						$src = imagecreatefromgif($uploadedfile);
					}
		 
					list($width,$height)=getimagesize($uploadedfile);

					$newwidth = 300;
					$newheight = 300;
					$tmp = imagecreatetruecolor($newwidth,$newheight);

					imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
					
					$imgLink = pathinfo($_FILES['imgfile']['name'], PATHINFO_FILENAME).$timeStamp.'.'.$extension;
					$filename = "Images/Listing/". $imgLink;
					
					imagejpeg($tmp,$filename,100);
					
					imagedestroy($src);
					imagedestroy($tmp);
				}
			}
		}
		else{
			$uploadError = true;
		}
	}
	else{
		$filename = $_FILES['imgfile']['name'];
		$extension = pathinfo($filename, PATHINFO_EXTENSION);
		$extension = strtolower($extension);
		$size = filesize($_FILES['imgfile']['tmp_name']);
		if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
			$extError = true;
		}
		else if ($size > MAX_SIZE*1024){
			$sizeError = true;
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Larger images not displaying triggering error

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Larger images not displaying triggering error

Posted 05 July 2011 - 01:11 AM

Hey.

If the upload_max_filesize directive is set to a lower value than the size of the uploaded image, the entry for it in the $_FILES array will be set but the tmp_name element will be empty, causing the filesize() function to return FALSE. - Your code only checks if the file size is larger than the specified max file size, but it doesn't make sure the file has a valid size.

You should always check to see if the $_FILES['filename']['error'] element is 0 before attempting to use the uploaded file, otherwise you may run into problems like these. See the section about handling file uploads in the PHP manual to see what the error numbers mean. (Specifically: Error Messages Explained)
Was This Post Helpful? 1
  • +
  • -

#3 scentauri  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 16-February 11

Re: Larger images not displaying triggering error

Posted 05 July 2011 - 01:32 AM

Thanks for the help!

I've made it work by adding the following IF statement to verify image size:

if (!$size || ($size > MAX_SIZE*1024))


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1