4

I need your help. I want to create a upload script with HTML, JQuery and PHP. Is it possible to write a script, that can upload very large files(> 5 GB)?

I've try it with FileReader, FormData and Blobs, but even with these, i can't upload large files(my browser crashes after selecting a large file).

PS: I want to write it myself. Don't post any finished scripts.

Regards

Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
Sylnois
  • 1,589
  • 6
  • 23
  • 47
  • it sounds like you may not appreciate the scope of the question. You should either use or at least start with an existing system, if you have to ask on SO to get started on this you're in over your head. Case in point is the browser crashing as out of the box behavior. – Matt Whipple Oct 29 '12 at 13:06
  • i can use objects like filereader? you don't understand completely. i just won't use plugin scripts like uploadify. – Sylnois Oct 29 '12 at 13:09
  • This question may be a duplicate - http://stackoverflow.com/questions/11590395/php-uploading-large-files – mtmacdonald Oct 29 '12 at 13:10
  • think about OS file system file limits. very important – Elzo Valugi Oct 29 '12 at 13:11
  • 3
    5GB through a webserver? Mmm. Sounds like the wrong tool for the job. – Lightness Races in Orbit Oct 29 '12 at 13:12
  • 2
    5GB is off the scale for a browser upload. – Sherlock Oct 29 '12 at 13:13
  • 3
    @user1515190 No...you don't understand completely. When's the last time you downloaded (which is more optimal than uploading) a 5GB file in a Web Browser or opened one in a program that didn't have memory management geared for working with large files. It's not a linear increase of work from a small file. – Matt Whipple Oct 29 '12 at 13:13
  • 2
    Where's megaupload when you need it...? – paddy Oct 29 '12 at 13:14
  • From SO: How about a Java applet? That's how we had to do it at a company I previously worked for. I know applets suck, especially in this day and age with all our options available, but they really are the most versatile solution to desktop-like problems encountered in web development. Just something to consider. http://stackoverflow.com/a/864588/1343222 check this – arun Oct 29 '12 at 13:21
  • Possible duplicate :http://stackoverflow.com/questions/864570/very-large-uploads-with-php – arun Oct 29 '12 at 13:23
  • i don't want/can use java applets. i need to use html, javascript and php. – Sylnois Oct 29 '12 at 13:33
  • http://stackoverflow.com/questions/13109296/php-upload-drops-with-big-files-w-o-error See this question too. – CodeAngry Oct 29 '12 at 13:35

5 Answers5

4

Yes. I wrote PHP to upload file exactly 5GB more then one year ago.

FileReader, FormData and Blobs will fail because they all require pre-process and convert in javascript before it get upload.

But, you can easily upload large file with plain simple XMLHttpRequest.

var xhr=new XMLHttpRequest();
xhr.send(document.forms[0]['fileinput']);

It is not a standard or documented way, however, few Chrome and Firefox do support. However, it send file content as is, not multipart/form-data, not http form-data. You will need to prepare your own http header to provide additional information.

var xhr=new XMLHttpRequest(), fileInput=document.forms[0]['fileinput'];
xhr.setRequestHeader("X-File-Name", encodeURIComponent(getInputFileName(fileInput)));
xhr.setRequestHeader("X-File-Size", getFileSize(fileInput));
xhr.send(fileInput);

PS. well, actually it was not PHP. It was mixed PHP and Java Servlet.

Dennis C
  • 24,511
  • 12
  • 71
  • 99
  • I tried to run your code but failed. I have a form with file input name file and tried xhr.send(document.forms[0].file); and also xhr.send(document.forms[0].['file']); but "xhr.send is not a function" error occures. And where to add the target script? Would xhr.open('POST', 'upload.php', true); after the declaration of var xhr be correct? – user2718671 Apr 07 '14 at 10:16
1

Look into "chunking", possibly with a plugin like AX Ajax multi uploader, which should help with both client and server-side file size limits.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
mtmacdonald
  • 14,216
  • 19
  • 63
  • 99
1

Keep in mind that it is important to adjust your PHP.ini variable (which is related to script timing), called Maximum execution time of each script, in seconds max_execution_time = xxxx in order to prevent your script from time out, because uploading large files is as you know time consuming. Check also variable max_input_time = xxxx, which is maximum amount of time each script may spend parsing request data. It's a good idea to limit this time on productions servers in order to eliminate unexpectedly long running scripts, but in your case you may need to increase it. Consider also changing following variables memory_limit, upload_max_filesize, post_max_size

Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52
1

The problem is, that its not really practical. First you have the problem, that you have to restart your upload when you have a browser problem. And this could happen when you upload a big file.

Here is another solution with Ajax:

php uploading large files
AX-JQuery Uploader

Community
  • 1
  • 1
René Höhle
  • 26,716
  • 22
  • 73
  • 82
1

I dont think web uploads were thought for 5gb+ kind of file, or that the browser is going to transfer this kind of information happily. File system limitation are also an issue. You should think/rethink the file upload depending on the usage scenario. Is the web only option? FTP, streaming, remote dumping are probably better solution that will not block your webserver/webpage while doing the transfer. HTTP is not the best protocol for this.

Think that the browser, PHP and Apache they all have limited memory. My antivirus warns me when chrome uses more than 250 MB per page (which is not considered normal). PHP has a default 128 MB of dedicated memory, and imagine having 100 simultaneous Apache users uploading 5GB files. That is why they invented FTP.

Why do you think those limits exists in PHP, apache...? Because is a way of attack, a security issue and a way of blocking the server which can be easily exploited by ... everybody.

Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
  • Sure http is not the way to go for big files +1 – A. Wolff Oct 29 '12 at 13:41
  • Why not? Even some people out there believe http is best and ready for everything. From classic Webservice/RESTful, Chrome OS, node.js, and many and many. – Dennis C Oct 29 '12 at 15:12