title: File download in php, memory limit problem?
body: <p>A client of mine decided to move the website from a somewhat nice server to a... lets call it lesser nice server.</p>
  
  <p>The problem is, there's a file with 40MBs to be downloaded and the memory limit on the server is 32. To make it even more difficult for me, they don't allow fopen...</p>
  
  <p>Also, if i reduce the file size to 20MB it works fine.</p>
  
  <p>So, my question is, what can i do - besides reducing the file size - to make this work?</p>
  
  <p>Thank you</p>
  
  <p>EDIT:`</p>
  
  <pre><code>        $fsize = filesize($file_path);
          $path_parts = pathinfo($file_path);
          $ext = strtolower($path_parts["extension"]);
  
  
          switch ($ext) {
              case "pdf": $ctype = "application/pdf";
                  break;
              case "exe": $ctype = "application/octet-stream";
                  break;
              case "zip": $ctype = "application/zip";
                  break;
              case "doc": $ctype = "application/msword";
                  break;
              case "xls": $ctype = "application/vnd.ms-excel";
                  break;
              case "ppt": $ctype = "application/vnd.ms-powerpoint";
                  break;
              case "gif": $ctype = "image/gif";
                  break;
              case "png": $ctype = "image/png";
                  break;
              case "jpeg":
              case "jpg": $ctype = "image/jpg";
                  break;
              default: $ctype = "application/force-download";
          }
  
          header("Pragma: public"); // required
          header("Expires: 0");
          header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
          header("Cache-Control: private", false); // required for certain browsers
          header("Content-Type: $ctype");
          header("Content-Disposition: attachment; filename=\"" . basename($file_path) . "\";");
          header("Content-Transfer-Encoding: binary");
          header("Content-Length: " . $fsize);
          ob_clean();
          flush();
          readfile($file_path);`
  </code></pre>
  
  <p>The code i saw on php.net</p>
  
  <pre><code>// If it's a large file, readfile might not be able to do it in one go, so:
  $chunksize = 1 * (1024 * 1024); // how many bytes per chunk
  if ($size &gt; $chunksize) {
    $handle = fopen($realpath, 'rb');
    $buffer = '';
    while (!feof($handle)) {
      $buffer = fread($handle, $chunksize);
      echo $buffer;
      ob_flush();
      flush();
    }
    fclose($handle);
  } else {
    readfile($realpath);
  }
  </code></pre>