John English
John English

Reputation: 43

Issue when uploading large images

Hi I have this which I picked from a tutorial. Link here Try help me understand this a bit.

  1. I want to know how the coder has attempted to control the size of the image uploading. (I've seen elsewhere in codes clearly restricting the size i.e ($_FILES["file"]["size"] < 20000))

  2. Reason why im asking the above question is when I upload a smaller image this works fine but a larger image gives a series of Warnings!

Thank you for your help.

Code

<?php

function getExtension($str)
{
 $i = strrpos($str,".");
 if (!$i) { return ""; }

 $l = strlen($str) - $i;
 $ext = substr($str,$i+1,$l);
 return $ext;
}

$image =$_FILES["imagefile"]["name"];
$tempext = getExtension($image);
$extfile= mt_rand().".".$tempext;
$uploadedfile = $_FILES['imagefile']['tmp_name'];

if ($extfile)
{
$filename = stripslashes($extfile);
$extension = getExtension($filename);   // return the type of image
$extension = strtolower($extension);    // convert result to lowercase
if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif"))
{
$errors=1;
}
else
{
$size=filesize($_FILES['imagefile']['tmp_name']);

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['imagefile']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['imagefile']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}

list($width,$height)=getimagesize($uploadedfile);

$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0, $newwidth, $newheight, $width, $height);

$newwidth1=185;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0, $newwidth1, $newheight1, $width, $height);

$filename = "upload/". $extfile;
$filename1 = "upload/thumbs/". $extfile;  
$etest1=imagejpeg($tmp,$filename,100);
$etest2=imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
?>

Upvotes: 1

Views: 1220

Answers (3)

BitParser
BitParser

Reputation: 3968

Without posting your warnings, we cannot determine what the real problem might be. However I suspect that is related to your php.ini file (it is the file where PHP keeps configurations).

The coder you mention on your link hasn't attempted to restrict the file size. Although you do not have an explicit statement in your code stating that the file size should not be larger than 20MB, this doesn't mean that you can upload a file of any size you wish.

In your case, there must be some directive in php.ini that prevents you from uploading files larger than 20M. Once you find php.ini (google where to find it, based on your OS) you can change the configuration of php.ini like below:

; Max size allowed for file upload
upload_max_filesize = 20M

; This must be greater than or equal to upload_max_filesize
post_max_size = 21M

Upvotes: 0

maybe this is better and more zipped

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2097152)) //2 MB

Upvotes: 1

Kashyap
Kashyap

Reputation: 391

try to use below example it is standard and will let you control on file size which you mentioned in your question (i.e. $_FILES["file"]["size"] < 20000) example here.

Upvotes: 0

Related Questions