简体   繁体   English

无需下载Google App引擎即可获取远程URL的文件大小(php)

[英]Get file size of remote url without downloading in Google app engine(php)

Hi want to check the file size of some remote file size , below csize function working normally in localhost.But when i hosted in google app engine i came to know there is not curl support.so i used purl wrapper.Still i am facing error. 您好想检查一些远程文件大小的文件大小,下面的csize函数可以在localhost中正常工作。但是当我托管在google app引擎中时,我知道不存在curl支持。所以我使用了purl包装器。仍然遇到错误。

I heard its possible to use java inside gae php file..if so is there any function in java to get file size of remote file ? 我听说有可能在gae php文件中使用java。如果是,那么java中是否有任何函数可以获取远程文件的文件大小? If so how to use it inside php. 如果是这样,如何在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;
}

Just use http streams API 只需使用HTTP Streams 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];
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM