开发者

How to get the progress status of the copy function in PHP?

开发者 https://www.devze.com 2023-03-01 09:36 出处:网络
I need to know h开发者_如何学Goow to get the status of the copy() function in PHP. I am using this function to download a remote file, and I want a progress bar for this program.You\'ll need to write

I need to know h开发者_如何学Goow to get the status of the copy() function in PHP.

I am using this function to download a remote file, and I want a progress bar for this program.


You'll need to write your own copy function. First check the file size through a HTTP HEAD request, for example with this solution:

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

Then do this to fetch the file:

$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);


You can't get a progress bar for a copy() call to my knowledge.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号