小编web*_*dev的帖子

PHP完全下载后停止读取远程文件

我收到了30秒的超时错误,因为代码一直在检查文件是否超过5mb.该代码旨在拒绝超过5mb的文件,但我需要它也在文件低于5mb时停止执行.有没有办法检查文件传输块是否为空?我目前正在使用DaveRandom的这个例子:

如果PHP超过5mb,则停止远程文件下载

代码:DaveRandom:

$url = 'http://www.spacetelescope.org/static/archives/images/large/heic0601a.jpg';
$file = '../temp/test.jpg';
$limit = 5 * 1024 * 1024; // 5MB

if (!$rfp = fopen($url, 'r')) {
  // error, could not open remote file
}
if (!$lfp = fopen($file, 'w')) {
  // error, could not open local file
}

// Check the content-length for exceeding the limit
foreach ($http_response_header as $header) {
  if (preg_match('/^\s*content-length\s*:\s*(\d+)\s*$/', $header, $matches)) {
    if ($matches[1] > $limit) {
      // error, file too large
    }
  }
}

$downloaded …
Run Code Online (Sandbox Code Playgroud)

php

6
推荐指数
1
解决办法
210
查看次数

如果PHP超过5mb,则停止远程文件下载

如果文件超过5mb,如何阻止远程文件下载文件?如果我在转移时停止它,文件是否会保存在某个其他临时目录或内存中?我怎么会知道?这是我目前的代码:

$url = 'http://www.spacetelescope.org/static/archives/images/large/heic0601a.jpg';
$file = '../temp/test.jpg';
file_put_contents($file, file_get_contents($url));
Run Code Online (Sandbox Code Playgroud)

php

5
推荐指数
1
解决办法
1704
查看次数

标签 统计

php ×2