栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

远程文件大小,无需下载文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

远程文件大小,无需下载文件

在这里找到有关此的一些信息:

这是(我发现)获取远程文件大小的最佳方法。请注意,HEAD请求不会获取请求的实际主体,它们只是检索标头。因此,对100MB的资源进行HEAD请求所花费的时间与对1KB的资源进行HEAD请求所花费的时间相同。

<?phpfunction curl_get_file_size( $url ) {  // Assume failure.  $result = -1;  $curl = curl_init( $url );  // Issue a HEAD request and follow any redirects.  curl_setopt( $curl, CURLOPT_NOBODY, true );  curl_setopt( $curl, CURLOPT_HEADER, true );  curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );  curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );  curl_setopt( $curl, CURLOPT_USERAGENT, get_user_agent_string() );  $data = curl_exec( $curl );  curl_close( $curl );  if( $data ) {    $content_length = "unknown";    $status = "unknown";    if( preg_match( "/^HTTP/1.[01] (ddd)/", $data, $matches ) ) {      $status = (int)$matches[1];    }    if( preg_match( "/Content-Length: (d+)/", $data, $matches ) ) {      $content_length = (int)$matches[1];    }    // http://en.wikipedia.org/wiki/List_of_HTTP_status_pres    if( $status == 200 || ($status > 300 && $status <= 308) ) {      $result = $content_length;    }  }  return $result;}?>

用法:

$file_size = curl_get_file_size( "http://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file" );


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465739.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号