maximum file size file_get_contents

Posted on 2008-11-05
Last Modified: 2013-12-13

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()?
Question by:cfpdev
    LVL 5

    Expert Comment


    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.          
    LVL 5

    Expert Comment

    Another usefull bit of information
    LVL 34

    Expert Comment

    by:Beverley Portlock
    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
    LVL 107

    Accepted Solution

    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

    Author Comment


    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.

    Author Closing Comment

    cheers ray.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to count occurrences of each item in an array.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now