【问题标题】:Get file size of remote url without downloading in Google app engine(php)无需在 Google 应用引擎(php)中下载即可获取远程 url 的文件大小
【发布时间】:2023-04-09 11:35:01
【问题描述】:

您好想检查一些远程文件大小的文件大小,下面的 csize 函数在 localhost 中正常工作。但是当我托管在谷歌应用引擎中时,我知道没有 curl 支持。所以我使用 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;
}

【问题讨论】:

    标签: java php google-app-engine curl


    【解决方案1】:

    只需使用 http 流 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];
          }
        }
      }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-05
    • 2011-11-19
    • 1970-01-01
    • 2013-08-07
    • 2016-03-23
    • 2012-06-23
    • 2012-11-19
    相关资源
    最近更新 更多