Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy large files (over 2 GB) in PHP

Tags:

file

php

copy

I need to copy some big file (6 GB) via PHP. How can I do that? The Copy() function can't do it.

I am using PHP 5.3 on Windows 32/64.

like image 361
Bauer01 Avatar asked Jul 03 '11 18:07

Bauer01


People also ask

How can I upload large files over 500mb in PHP?

By changing the upload_max_filesize limit in the php. ini file. By implementing file chunk upload, that splits the upload into smaller pieces an assembling these pieces when the upload is completed.

What is the fastest way to copy large files?

Courier a Hard Drive If you have lots of large files to send then filling up a hard drive and sending it off with a courier is an effective way to transfer files. Physically sending a large volume of files via a courier is often much faster than attempting to upload those files through a broadband connection.

How do I copy a large amount of files?

But you can still use a few ways to copy and paste faster. Hold Ctrl and click multiple files to select them all, no matter where they are on the page. To select multiple files in a row, click the first one, then hold Shift while you click the last one. This lets you easily pick a large number of files to copy or cut.

How copy data from file in PHP?

The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.


1 Answers

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
}
like image 189
Dogbert Avatar answered Oct 04 '22 17:10

Dogbert