PHP 获取远程文件大小的3种解决方法
发布时间:2020-12-13 06:30:37 所属栏目:PHP教程 来源:网络整理
导读:1、使用file_get_contents() 代码如下: $file = file_get_contents($url); echo strlen($file); ?> 2. 使用get_headers() 代码如下: $header_array = get_headers($url,true); $size = $header_array['Content-Length']; echo $size; ?> 需要打开allow_url_f
1、使用file_get_contents() 代码如下: $file = file_get_contents($url); echo strlen($file); ?> 2. 使用get_headers() 代码如下: $header_array = get_headers($url,true); $size = $header_array['Content-Length']; echo $size; ?> 需要打开allow_url_fopen!如未打开会显示Warning: get_headers() [function.get-headers]: URL file-access is disabled in the server configuration 3.使用fsockopen() 代码如下: function get_file_size($url) { $url = parse_url($url); if (empty($url['host'])) { return false; } $url['port'] = empty($url['post']) ? 80 : $url['post']; $url['path'] = empty($url['path']) ? '/' : $url['path']; $fp = fsockopen($url['host'],$url['port'],$error); if($fp) { fputs($fp,"GET " . $url['path'] . " HTTP/1.1rn"); fputs($fp,"Host:" . $url['host']. "rnrn"); while (!feof($fp)) { $str = fgets($fp); if (trim($str) == '') { break; }elseif(preg_match('/Content-Length:(.*)/si',$str,$arr)) { return trim($arr[1]); } } fclose ( $fp); return false; }else { return false; } } ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |