Copy large files (over 2 GB) in PHP

前端 未结 6 871
执笔经年
执笔经年 2020-12-09 10:18

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.

相关标签:
6条回答
  • 2020-12-09 11:05

    You could use exec() if it's a linux machine.

    $srcFile = escapeshellarg($pathToSrcFile);
    $trgFile = escapeshellarg($pathToTrgFile);
    
    exec("cp $srcFile $trgFile");
    
    0 讨论(0)
  • 2020-12-09 11:07

    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
    }
    
    0 讨论(0)
  • 2020-12-09 11:09

    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.

    0 讨论(0)
  • 2020-12-09 11:12

    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+')
    );
    
    0 讨论(0)
  • 2020-12-09 11:14

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

    0 讨论(0)
  • 2020-12-09 11:15

    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).

    0 讨论(0)
提交回复
热议问题