49

I have used following code to generate zip

// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);

this code works fine but for unknown reasons was not working until I tried

// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
echo file_get_contents($zip_name);

I am curious about finding what is happening in both the cases

justnajm
  • 4,422
  • 6
  • 36
  • 56

1 Answers1

83

Readfile will read the file directly into the output buffer, and file_get_contents will load the file into memory, when you echo the result the data is copied from memory to the output buffer effectively using 2 times the memory of readfile.

Jesper Blaase
  • 2,320
  • 16
  • 13
  • 3
    Yes this is what my observation is that readfile was trying to throw file while it was generating, but file_get_contents wait till it get genrated successfully. – justnajm Nov 20 '13 at 12:01