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.
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.
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+')
);
You could use exec()
if it's a linux machine.
$srcFile = escapeshellarg($pathToSrcFile);
$trgFile = escapeshellarg($pathToTrgFile);
exec("cp $srcFile $trgFile");
I would copy it X byte by X byte (several megs each iteration).
X will be the most optimized size which depends on your machine.
And I would do it not through the web server but as a stand alone script, run through cron or one time call to it (cli).
If you want to copy files from one server to another and you have ftp access on both of them, then you can simply use ftp 'put' command on source system and send the big file to the other system easily.