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

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5323
  • Last Modified:

maximum file size file_get_contents


Large files (250mb+) are stored on local filesystem using obsfucated filenames. File meta data (eg original filename) is stored in a database.

A PHP script uses file_get_contents() to force the download of the file to a logged in user, prompting the user to download the file with the original filename.

What is the maximum filesize that can be piped through the php function file_get_contents()?
1 Solution

This was taken from

post_max_size         integer                                 Sets max size of post data allowed. This setting also affects         file upload. To upload large files, this value must be larger         than upload_max_filesize.                         If memory limit is enabled by your configure script, memory_limit also affects         file uploading. Generally speaking,         memory_limit should be         larger than post_max_size .                 When an integer is used, the  value is measured in bytes. Shorthand notation, as described  in this FAQ, may also be used.                   If the size of post data is greater than post_max_size, the         $_POST and $_FILES         superglobals         are empty. This can be tracked in various ways, e.g. by passing the         $_GET variable to the script processing the data,         i.e. <form action="edit.php?processed=1">,         and then checking if $_GET['processed'] is set.                
Note:                      PHP allows shortcuts for bit values, including K (kilo), M (mega)            and G (giga). PHP will do the conversions automatically if you            use any of these. Be careful not to exceed the 32 bit signed integer            limit (if you're using 32bit versions) as it will cause your script            to fail.          
Another usefull bit of information

Beverley PortlockCommented:
Something else to bear in mind is that there was a 32bit integer problem with some PHP file handling which limits files to 2GB in size. I wouldn't recommend uploading them, but if you are manipulating them on disc bear it in mind.

IIRC the problem was fixed in PHP5.1 or 5.2
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Ray PaseurCommented:
There may also be limits imposed by your hosting service on memory utilization.  file_get_contents() will try to load the entire file into memory, and this may trip the lever.  Best to ask your host about that.  A workaround is to process the file incrementally, using record functions such as fgets() or fread().

Good luck, ~Ray
cfpdevAuthor Commented:

I'm not talking about POST/file uploads. Thanks for the info anyway.


Files won't be more than hundreds of megabytes.


You're not wrong re file_get_contents and memory handling. This seems a pretty good solution using fread:

My initial tests on vmware seem pretty favourable using this method. While not as good as a straight link to the file using apache, it's a lot less memory intensive than file_get_contents.
cfpdevAuthor Commented:
cheers ray.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now