Copy large files (over 2 GB) in PHP Copy large files (over 2 GB) in PHP php php

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}


Recent versions of PHP copy files with chunks so today you could use php copy() function safely


If copy doesnt work, you can try with

Example

stream_copy_to_stream(    fopen('/path/to/input/file.txt', 'r'),    fopen('/path/to/output/file.txt', 'w+'));

Also see https://bugs.php.net/bug.php?id=81145