首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取远程url的文件大小,而无需在Google (Php)中下载。

问获取远程url的文件大小,而无需在Google (Php)中下载。
EN

Stack Overflow用户
提问于 2014-11-23 22:01:24
回答 1查看 240关注 0票数 0

Hi想检查一些远程文件大小的文件大小,在localhost.But中正常工作的csize函数下,当我在google引擎中托管时,我发现我没有使用support.so,我使用了purl wrapper.Still,我遇到了错误。

我听说可以在gae file..if中使用java,那么在java中是否有任何函数来获取远程文件的文件大小?如果是这样的话,如何在php中使用它。

代码语言:javascript
复制
<?php

require_once 'Purl.php';


echo csize('http://www.example.com');


function csize($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
return $size;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-24 06:10:29

只需使用http streams API

代码语言:javascript
复制
function csize($url) {
  $options = ['http' => [
      'method' => 'HEAD',
    ],
  ];
  $ctx = stream_context_create($options);
  $result = file_get_contents($url, false, $ctx);
  if ($result !== false) {
    foreach($http_response_header as $header) {
      if (preg_match("/Content-Length: (\d+)/i", $header, $matches)) {
        return $matches[1];
      }
    }
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27094889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档