title: PHP Memory Limit
body: <p>Would anyone be able to give me a quick primer on how memory resources are used?  I know I can up the PHP memory limit in PHP.ini as well as through lines of code such as:</p>
  
  <pre><code>    ini_set("memory_limit","24M");
  </code></pre>
  
  <p>I have an image upload script that I'm writing that is making use of a pretty cool PHP script called simpleImage which can be found here: <a href="http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php" rel="nofollow">http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php</a></p>
  
  <p>I have a basic form that accepts JPGs and PNGs.  I've set my PHP memory limit to 24m, thinking this would be high enough, but when I tried to upload a 3mb image I still received the beautiful memory allocation exhausted error.  Bumping the value up to a meatier 240M and the script runs fine (locally).</p>
  
  <p>My script does this:<br/>
  1) Accepts the TMP Image being uploaded<br/>
  2) Runs getimagesize() on the image to check that it's a valid image.<br/>
  3) Moves the temporary image to the final destination directory.<br/>
  4) Loads the image using simple image.<br/>
  5) Resizes the image using simple image.<br/>
  6) Saves the resized image using simple image script.</p>
  
  <p>So I guess that all the loading/checking/resizing requires a bit more than the 24M.  But I'm worried about what an acceptable memory limit allocation would be.  I would like for users to comfortable be able to upload ~6MB images.  Is this going to be extremely stressful on your average dedicated servers?</p>
  
  <p>Here's the gist of my script.. personally I don't think it's excessively wasteful on resources??</p>
  
  <pre><code>    if (!empty($_FILES)) {
      // get image
      ini_set("memory_limit","24M");
      require_once('simpleImage.php');
      require_once('db.php');
      $tempFile             =     $_FILES['file']['tmp_name'];
      $originalFile        =    $_FILES['file']['name'];
      $extension             =     strtolower(end(explode(".", $originalFile)));
      $targetFile         =    "path/to/directory/";
  
      // validate image
      $validExtensions    =    array('jpg', 'jpeg', 'png');
      if(in_array($extension, $validExtensions)) {
          $validExtension        =    true;    
      } else {
          $validExtension        =    false;    
      }
  
      if(getimagesize($tempFile) == false) {
          $validImage        =    false;    
      } else {
          $validImage        =    true;    
      }
  
  
      if($validExtension == true &amp;&amp; $validImage == true) {
          if(move_uploaded_file($tempFile,$targetFile)) {
  
              $image = new SimpleImage();
              $image-&gt;load($targetFile);
              $image-&gt;resizeToWidth(500);
              $image-&gt;save($targetFile);
          }
      } 
  }
  </code></pre>