alexanderfoti
asked on
php image upload memory size error
Hi there,
i have a script that is uploading images to my server when trying to upload a 2.4MB image called img443.jpg i get the error
'ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is .'
with other images that have a lower case file extension, i get the error:
'Image uploaded successfully.
Fatal error: Allowed memory size of 12582912 bytes exhausted (tried to allocate 3072 bytes) in /opt/lampp/htdocs/nahon.co .uk/manage pics.php on line 25'
this means the original size image is uploaded but the script fails to generate the thumbnail.
i have set the memory limit to 12mb in the script, does it need to be set in php.ini?
thanks,
i have a script that is uploading images to my server when trying to upload a 2.4MB image called img443.jpg i get the error
'ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is .'
with other images that have a lower case file extension, i get the error:
'Image uploaded successfully.
Fatal error: Allowed memory size of 12582912 bytes exhausted (tried to allocate 3072 bytes) in /opt/lampp/htdocs/nahon.co
this means the original size image is uploaded but the script fails to generate the thumbnail.
i have set the memory limit to 12mb in the script, does it need to be set in php.ini?
thanks,
<?php
$idir = "demo/img/original/"; // Path To Images Directory
$tdir = "demo/img/thumbs/"; // Path To Thumbnails Directory
$twidth = "100"; // Maximum Width For Thumbnail Images
$theight = "100"; // Maximum Height For Thumbnail Images
if (!isset($_GET['subpage'])) { // Image Upload Form Below ?>
<form method="post" action="managepics.php?subpage=upload" enctype="multipart/form-data">
<input type="file" name="imagefile" class="form">
<br /><br />
<input name="submit" type="submit" value="Sumbit" class="form"> <input type="reset" value="Clear" class="form">
</form>
<?php } else if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') { // Uploading/Resizing Script
//$url = str_replace(' ', '_', $_FILES['imagefile']['name']);
$url = strtolower(str_replace(' ', '_', $_FILES['imagefile']['name']));
if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
//$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location
$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . strtolower(str_replace(' ', '_', $_FILES['imagefile']['name']))); // Move Image From Temporary Location To Permanent Location
ini_set("memory_limit","12M");
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
print 'Image thumbnail created successfully.'; // Resize successful
} else {
print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed
}
} else {
print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong
print $file_ext; // Show The Invalid File's Extention
print '.</font>';
}
} ?>
ASKER
where can i check these or set them to a higher value? the file is only 2.4mb and the error states 12mb allowed memory size.
These are in your php.ini file. This is usually in a location like /etc/php5/apache2. If you execute phpinfo() in your script and scan for php.ini it should give you the location of the file.
Many php.ini files set upload_max_filesize to 2M by default. memory_limit is in there too.
Many php.ini files set upload_max_filesize to 2M by default. memory_limit is in there too.
ASKER
so phpinfo() shows upload_max_filesize is 2mb.
my site is hosted with a hosting company, is there any way round this?
my site is hosted with a hosting company, is there any way round this?
You should be able to set that value in a .htaccess file like so
php_value post_max_size 5M
php_value post_max_size 5M
I should add that some hosting companies block or disallow some commands in .htaccess but you will not know until you try it.
Put it in .htaccess and do a phpinfo() and check the 2nd column - the first is the default setting and the 2nd Column is the current setting.
Put it in .htaccess and do a phpinfo() and check the 2nd column - the first is the default setting and the 2nd Column is the current setting.
ASKER
i have now set three variables to be 8M: memory_limit, post_max_size and upload_max_filesize
phpinfo confirms that the values are set
i am getting the error
Fatal error: Allowed memory size of 12582912 bytes exhausted (tried to allocate 12288 bytes) in /opt/lampp/htdocs/nahon.co .uk/manage pics.php on line 25
the original file is uploaded but this error is for generating the thumbnail
phpinfo confirms that the values are set
i am getting the error
Fatal error: Allowed memory size of 12582912 bytes exhausted (tried to allocate 12288 bytes) in /opt/lampp/htdocs/nahon.co
the original file is uploaded but this error is for generating the thumbnail
set the memory limit much bigger - 32M or 64M
ASKER
i have set the memory limit to 128M and am getting the same error
Fatal error: Allowed memory size of 12582912 bytes exhausted (tried to allocate 12288 bytes) in /opt/lampp/htdocs/nahon.co .uk/manage pics.php on line 25
Fatal error: Allowed memory size of 12582912 bytes exhausted (tried to allocate 12288 bytes) in /opt/lampp/htdocs/nahon.co
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks, i have spoken to them and they have sorted the issue, though did not tell me what it was...
OK - pure guesswork time...... Chances are that your memory limit was way, way too low. My understanding is that FastHosts had a 20MB limit rather than 12MB on some of its hosting plans. Was it a server that you have had for some time? PHP4 used to be set to 8MB in its out of the box configuration.
I could understand if you had this for some years as 12MB was the sort of setting that was used 4 or 5 years ago.
I could understand if you had this for some years as 12MB was the sort of setting that was used 4 or 5 years ago.
post_max_size - the maximum size of a POST operation. Values like 8M are typical
upload_max_filesize - the maximum size of a file being uploaded.