Uploading Large Files With PHP

Let me first start by saying that I’m currently working at a startup, and for anyone who’s ever done this, you know it sucks up a LOT of your time… in my case pretty much all of it. I’m not complaining by any means, I’m doing some of the coolest work I’ve ever done in fact! However, I just wanted to explain my little posting hiatus…

Anyway, on to the matter at hand. I have found myself in a position where I am writing scripts that may need to upload fairly large files. My scripts were timing out, and I couldn’t seem to figure out why. For the unitiated, there are some standard things that you usually do to both your php.ini and in your script in this situation:

  • Set your max_upload_size in php.ini higher (it’s only 2M out of the box)
  • Set your script timeout to never (set_time_limit(0); in your scripts… don’t do this in your php.ini)

However, it turns out there are some other php.ini config variables that you may need to look at:

  • memory_limit – This may also be an obvious one to some people, but for those of you who don’t know, this restricts how much memory PHP is allowed to consume while processing. When working with images and large files, this needs to be upped to accommodate these needs.
  • post_max_size – This was the one killing me, and it was a major “Duh” moment as well. If you aren’t allowed to create a large post, how can you expect to upload a large file?? This should be set the same as your max_upload_size.
  • max_input_time – the time that the script should spend in accepting input. This is setting defaults to 60 seconds, and you will probably need to update this as well

That’s about it! Quick and simple solution to a problem that is probably pretty common.

Other's Thoughts   (20 so far...)


  • Lorenzo
    May 5 '08 at 7:49 am

    Pretty good work! I found myself in the same boring situation and reached the shore doing exactly what you describe in this post… But what if sizes are really large? I.E. : 50M. How can we implement a systerm that allows such an upload without crash our system?


  • bryan
    May 27 '08 at 10:18 am

    Hey thanks for this.

    I was having the same issue.
    I changed the max_upload_size but not the others. Stupid how they rely on each other. But it works now!


  • Charlie
    Aug 29 '08 at 12:23 pm

    Very useful, thanks. There are several articles out there for “large uploads via PHP” but they seem to only have partial settings mentioned.

    It’s very important that post_max_size is > max_upload_size. If only by a small amount.

    I’m in the process of setting up an app that will need to allow uploads up to 500M. — Lorenzo, follow these instructions and you should be ok. Also make sure to set in your form (where value= whatever size you need in bytes)


  • insic
    Sep 3 '08 at 8:50 am

    im still troubled and cant get my uploader work. still uploads only below 2M my hosting is 1and1 and i cant make it work ‘duh!’


  • Anton
    Sep 9 '08 at 12:37 am

    Thank you very much! This article solved all my problems!


  • Tom
    Sep 10 '08 at 4:03 am

    where do you put the php.ini file?


  • Lucie
    Sep 21 '08 at 12:31 pm

    Nice Article, Just followed each steps that are given here, I got the output correctly, Thanks a lot again.


  • Lucie
    Sep 21 '08 at 12:32 pm

    You can find the php.ini file in BIN folder, all you need to do is, change the max post size and max upload file size in your ini file


  • The White Wolf
    Sep 22 '08 at 8:04 pm

    This article is just what I needed – right to the point and thorough… I applied what it said in my php.ini file, and it worked like a charm (the post_max_size threw me off too)


  • Darrell
    Oct 17 '08 at 1:44 pm

    Create on php.ini file in your root with only the settings for adjusting file limits, then log into shell and create symbolic links to php.ini inside of any folder and sub folder that contains pieces of the upload script and files uploaded.


  • Darrell
    Oct 17 '08 at 1:45 pm

    My previous post was to insic about 1and1 hosting. Forgot to mention that.


  • Alex
    Oct 28 '08 at 12:11 pm

    I edited the php.ini to what you say and I’ve no problem in uploading small files. Although I have when I upload files>1.xMB (even if limits are set to 6-7 MB).
    Any help anybody?


  • Another Alex
    Nov 9 '08 at 3:47 am

    Alex,
    do you use MAX_FILE_SIZE directive in your form?


  • Danesh
    Dec 16 '08 at 9:41 am

    Thanks dude. Worked!


  • Adam
    Jan 26 '09 at 9:37 pm

    Is there an assumption that you’re using dedicated hosting or can these overrides be used in a shared hosting environment?

    I’ve built something similar using ASP but it too hits specific limits – and these cannot be worked around in most shared hosting environments for obvious reasons. Which is why I’m looking for alternatives.

    Thanks, Adam

    p.s. Please don’t bother to rant about PHP vs ASP – I’m sure we’ve all heard it before.


  • Ian
    Jan 26 '09 at 10:04 pm

    Well, in PHP you can override some of these settings in a .htaccess file… if there’s an equivalent methodology in ASP, I’d suggest going that route


  • slowdusk
    Feb 15 '09 at 2:59 pm

    Hey, thanx a lot…………I was having the same problem………I changed upload_max_filesize and left the others………but after following your instructions…..its working fine…………..thx a lot


  • Jakes
    Aug 16 '09 at 9:05 am

    Thank you, I love you! I have never said that to another man before. Thank you!


  • Ian
    Aug 18 '09 at 10:48 pm

    No problem, happy to help ;)


  • Kirill
    Oct 16 '09 at 4:01 am

    Hi.

    I have trouble uploading realy big files. Up to 5 GB.

    I set

    php_value upload_max_filesize 10000M
    php_value post_max_size 10000M
    php_value max_execution_time 3600
    php_value max_input_time -1
    php_value memory_limit -1

    LimitRequestBody 0

    I am able to upload 1.4 GB file, but failed in uploading 4 GB.

    Do you have any clue what to do?

  • Share Your Thoughts...

    Some HTML is ok. If this is your first comment on my site, it will be reviewed before being posted publicly. Your comment may be edited or marked as spam if it appears intended for SEO purposes.