Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

php image upload memory size error

Posted on 2010-01-07
12
Medium Priority
?
473 Views
Last Modified: 2013-12-25
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/managepics.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,
<?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>';
  }
} ?>

Open in new window

0
Comment
Question by:alexanderfoti
  • 7
  • 5
12 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26203186
There is another two limits you need to consider and these are

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.
0
 
LVL 1

Author Comment

by:alexanderfoti
ID: 26204405
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.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26204700
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:alexanderfoti
ID: 26204802
so phpinfo() shows upload_max_filesize is 2mb.

my site is hosted with a hosting company, is there any way round this?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26204937
You should be able to set that value in a .htaccess file like so

php_value post_max_size 5M
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26204961
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.
0
 
LVL 1

Author Comment

by:alexanderfoti
ID: 26204975
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/managepics.php on line 25

the original file is uploaded but this error is for generating the thumbnail
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26205057
set the memory limit much bigger - 32M or 64M
0
 
LVL 1

Author Comment

by:alexanderfoti
ID: 26205863
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/managepics.php on line 25
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 2000 total points
ID: 26208858
It seems that your hoster (fasthosts) limit the memory available regardless of your settings in php.ini so you may need to speak to their support people to see how to get this limit increased.
0
 
LVL 1

Author Comment

by:alexanderfoti
ID: 26209013
thanks, i have spoken to them and they have sorted the issue, though did not tell me what it was...
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26209329
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.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Over time, the online landscape has altered considerably, but that’s nothing compared to the up-and-coming trends that will shape the web design industry in the coming year. Keep reading to find out which trends will shape B2B web design in 2018.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question