无需在Google应用引擎(php)中下载即可获取远程网址的文件大小

时间:2014-11-23 22:01:24

标签: java php google-app-engine curl

你想检查一些远程文件大小的文件大小,csize函数正常工作在localhost.But当我在谷歌应用程序引擎托管时,我知道没有卷曲支持。所以我使用purl包装。仍然我面临错误。

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

<?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;
}

1 个答案:

答案 0 :(得分:2)

只需使用http stream API

即可
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];
      }
    }
  }
}
相关问题