Copy large files (over 2 GB) in PHP
This should do it.
function chunked_copy($from, $to) {
# 1 meg at a time, you can adjust this.
$buffer_size = 1048576;
$ret = 0;
$fin = fopen($from, "rb");
$fout = fopen($to, "w");
while(!feof($fin)) {
$ret += fwrite($fout, fread($fin, $buffer_size));
}
fclose($fin);
fclose($fout);
return $ret; # return number of bytes written
}
How to copy very large files from URL to server via PHP?
I think the problem might be the 30 second time-out on many servers running PHP scripts.
PHP scripts running via cron or shell wont have that problem so perhaps you could find a way to do it that way.
Alternatively you could add set_time_limit([desired time]) to the start of your code.
Reading very large files in PHP
Are you sure that it's fopen
that's failing and not your script's timeout setting? The default is usually around 30 seconds or so, and if your file is taking longer than that to read in, it may be tripping that up.
Another thing to consider may be the memory limit on your script - reading the file into an array may trip over this, so check your error log for memory warnings.
If neither of the above are your problem, you might look into using fgets
to read the file in line-by-line, processing as you go.
$handle = fopen("/tmp/uploadfile.txt", "r") or die("Couldn't get handle");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
// Process buffer here..
}
fclose($handle);
}
Edit
PHP doesn't seem to throw an error, it just returns false.
Is the path to $rawfile
correct relative to where the script is running? Perhaps try setting an absolute path here for the filename.
How to upload large files above 500MB in PHP
Do you think if increasing upload size limit will solve the problem? what if uploading 2GB file, what's happening then? Do you take into consideration the memory usage of such a script?
Instead, what you need is chunked upload, see here : Handling plupload's chunked uploads on the server-side
and here : File uploads; How to utilize "chunking"?
How can I download files larger than 2Gb in PHP?
I fixed the problem in the end by eliminating the readfile()
function and outputting the content of the file myself using fopen()
and fread()
.
However the reason why readfile()
' fails when dealing with such large files remains an open subject (I've read the documentation on php.net and I couldn't find any notes about such issue). So i'm still waiting for that experienced programmer to enlighten me :).
Very large uploads with PHP
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.
Related Topics
Wordpress Woocommerce - Use Update_Post_Meta to Add Product Attributes
Subtracting a Certain Number of Hours, Days, Months or Years from Date
Interview Question: in PHP, Is 123==0123
How to Force Page Not to Be Cached in PHP
Replacing .PHP Ext with .HTML Through .Htaccess
Call Python from PHP and Get Return Code
Laravel Says "Route Not Defined"
Remove Everything from the First Occurrence of a Character to the End of a String in PHP
Compile Error: "G++: Error Trying to Exec 'Cc1Plus': Execvp: No Such File or Directory"
PHP Image Resize on the Fly VS Storing Resized Images
Safe Alternatives to PHP Globals (Good Coding Practices)
How to Upload Multiple Image in Laravel