问题
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.
回答1:
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
}
回答2:
If copy doesnt work, you can try with
- stream_copy_to_stream — Copies data from one stream to another
Example
stream_copy_to_stream(
fopen('/path/to/input/file.txt', 'r'),
fopen('/path/to/output/file.txt', 'w+')
);
回答3:
Recent versions of PHP copy files with chunks so today you could use php copy() function safely
回答4:
You could use exec() if it's a linux machine.
$srcFile = escapeshellarg($pathToSrcFile);
$trgFile = escapeshellarg($pathToTrgFile);
exec("cp $srcFile $trgFile");
回答5:
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).
回答6:
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.
来源:https://stackoverflow.com/questions/6564643/copy-large-files-over-2-gb-in-php