php获取远程文件内容与大小的代码
- <?
- /**
- 获取远程文件内容
- @param $url 文件http地址
- */
- function fopen_url($url)
- {
- if (function_exists(‘file_get_contents‘)) {
- $file_content = @file_get_contents($url);
- } elseif (ini_get(‘allow_url_fopen‘) && ($file = @fopen($url, ‘rb‘))){
- $i = 0;
- while (!feof($file) && $i++ < 1000) {
- $file_content .= strtolower(fread($file, 4096));
- }
- fclose($file);
- } elseif (function_exists(‘curl_init‘)) {
- $curl_handle = curl_init();
- curl_setopt($curl_handle, CURLOPT_URL, $url);
- curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
- curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl_handle, CURLOPT_FAILONERROR,1);
- curl_setopt($curl_handle, CURLOPT_USERAGENT, ‘Trackback Spam Check‘); //引用垃圾邮件检查
- $file_content = curl_exec($curl_handle);
- curl_close($curl_handle);
- } else {
- $file_content = ‘‘;
- }
- return $file_content;
- }
- ?>
- <?php
- /**
- * 获取远程文件大小
- * edit by www.jbxue.com
- */
- function getFileSize($url){
- $url = parse_url($url);
- if($fp = @fsockopen($url[‘host‘],empty($url[‘port‘])?80:$url[‘port‘],$error)){
- fputs($fp,"GET ".(empty($url[‘path‘])?‘/‘:$url[‘path‘])." HTTP/1.1\r\n");
- fputs($fp,"Host:$url[host]\r\n\r\n");
- while(!feof($fp)){
- $tmp = fgets($fp);
- if(trim($tmp) == ‘‘){
- break;
- }else if(preg_match(‘/Content-Length:(.*)/si‘,$tmp,$arr)){
- return trim($arr[1]);
- }
- }
- return null;
- }else{
- return null;
- }
- }
- //调用方法
- echo getFileSize("http://www.jbxue.com/images/logo.gif")
- ?>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。