Php从远程URL复制视频并显示进度

时间:2015-10-18 06:35:07

标签: php copy progress-bar

我想从远程URL复制文件,特别是视频文件,但也希望显示复制的进度。

我目前的代码是:

$remote = fopen('remote-file', 'r');
$local = fopen('local-file', 'w');

    $read_bytes = 0;
    while(!feof($remote)) {
      $buffer = fread($remote, 2048);
      fwrite($local, $buffer);

      $read_bytes += 2048;

      //Use $filesize as calculated earlier to get the progress percentage
      $progress = min(100, 100 * $read_bytes / $filesize);
      //you'll need some way to send $progress to the browser.
      //maybe save it to a file and then let an Ajax call check it?
    }
    fclose($remote);
    fclose($local);

要获取文件大小,我正在使用此方法。

http://php.net/manual/en/function.filesize.php#92462

但是我获得的6.58 MB文件的文件大小是1056,因此它总是显示进度为100。

我应该如何获得正确的远程URL文件大小?

0 个答案:

没有答案