php获取远程文件大小
方法1:get_headers
- <?php
- get_headers($url,true);
- //返回结果
- Array
- (
- [0] => HTTP/1.1 200 OK
- [Date] => Sat, 29 May 2004 12:28:14 GMT
- [Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
- [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
- [ETag] => "3f80f-1b6-3e1cb03b"
- [Accept-Ranges] => bytes
- [Content-Length] => 438
- [Connection] => close
- [Content-Type] => text/html
- )
- ?>
此处可以直接根据Content-Length来获取到远程文件的大小了.
方法2:curl
- function remote_filesize($uri,$user='',$pw='')
- {
- // start output buffering
- ob_start();
- // initialize curl with given uri
- $ch = curl_init($uri);
- // make sure we get the header
- curl_setopt($ch, CURLOPT_HEADER, 1);
- // make it a http HEAD request
- curl_setopt($ch, CURLOPT_NOBODY, 1);
- // if auth is needed, do it here
- if (!emptyempty($user) && !emptyempty($pw))
- {
- $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- $okay = curl_exec($ch);
- curl_close($ch);
- // get the output buffer
- $head = ob_get_contents();
- // clean the output buffer and return to previous
- // buffer settings
- ob_end_clean();
- echo '<br>head-->'.$head.'<----end <br>';
- // gets you the numeric value from the Content-Length
- // field in the http header
- $regex = '/Content-Length:\s([0-9].+?)\s/';
- $count = preg_match($regex, $head, $matches);
- // if there was a Content-Length field, its value
- // will now be in $matches[1]
- if (isset($matches[1]))
- {
- $size = $matches[1];
- }
- else
- {
- $size = 'unknown';
- }
- //$last=round($size/(1024*1024),3);
- //return $last.' MB';
- return $size;
- }
方法3:socket
- function getFileSize($url)
- {
- $url = parse_url($url);
- if($fp = @fsockopen($url['host'],emptyempty($url['port'])?80:$url['port'],$error))
- {
- fputs($fp,"GET ".(emptyempty($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;
- }
- elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr))
- {
- return trim($arr[1]);
- }
- }
- return null;
- }
- else
- {
- return null;
- }
- }
方法4:file_get_contents
- $fCont = file_get_contents("http://www.mg27.com/1.html");
- echo strlen($fCont)/1024;
以上四种方法
curl > fsock > file_get_contents > getheader
php获取远程文件大小的更多相关文章
- php下载远程大文件(获取远程文件大小)
function download_file($url) { // $url = http://192.168.8.95/vm/download_file?downurl=http://192.168 ...
- PHP获取远程和本地文件信息(汇总)
1.PHP filesize() 函数filesize() 函数返回指定文件的大小.若成功,则返回文件大小的字节数.若失败,则返回 false 并生成一条 E_WARNING 级的错误. 但是只能获取 ...
- php获取远程图片并把它保存到本地
/* *功能:php多种方式完美实现下载远程图片保存到本地 *参数:文件url,保存文件名称,使用的下载方式 *当保存文件名称为空时则使用远程文件原来的名称 */ function getImage( ...
- 使用JCIFS获取远程共享文件
package com.jadyer.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOExc ...
- curl获取远程图片存到本地
$url = 'http://sssss/sss/xu0fLo9waqKSTDO7j0kSO41O5Luq3LB6ozUvY4O7OsXUWNicB49fBs8nGYzoqcwGDARQZHpVuic ...
- php 获取远程图片保存到本地
php 获取远程图片保存到本地 使用两个函数 1.获取远程文件 2.把图片保存到本地 /** * 获取远程图片并把它保存到本地 * $url 是远程图片的完整URL地址,不能为空. */ functi ...
- 解析PHP中的file_get_contents获取远程页面乱码的问题【转】
在工作中,遇到一个问题.我需要将一个网址(该网址是一个json数据的接口,即 打开该网址,在浏览器中显示的是json数据),我使用file_get_contents($url),数据是乱码的. 通过查 ...
- scp命令获取远程文件
一.scp是什么? scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的,可能会稍微影响 ...
- PHP高效获取远程图片尺寸和大小(转)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
随机推荐
- mysql使用笔记(三)
一.数值类型 1. 数值类型 标准sql中包含数据类型 INTEGER, SMALLINT, DECIMAL, NUMERIC,以及FLOAT, REAL, DOUBLE.mysql在此基础上 ...
- EXEL表格读取 按键精灵
EXEL表格读取(1,m)(2,m)表格信息,m为行数 以下为本帖隐藏内容 ============================== Call Plugin.Office.OpenXls(&quo ...
- java 集合(set)
Interface ListIterator<E> 特有的方法: hasPrevious() 判断是否存在上一个元素. previous() 当前指针先向上移动一个单位,然后再取出当前指针 ...
- Eclipse全面提速小技巧
转自:http://rongmayisheng.com/post/eclipse%E5%85%A8%E9%9D%A2%E6%8F%90%E9%80%9F 欢迎关注我的社交账号: 博客园地址: http ...
- 【CITE】C#默认以管理员身份运行程序实现代码
//用于一种情况:C#软件打包后,在读写C盘文件时,会出现权限问题.使用管理员身份才可以运行 using System; using System.Collections.Generic; using ...
- 二级菜单jquery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 你不知道的JavaScript-- 事件流与事件处理
转载:http://blog.csdn.net/i10630226/article/details/48970971 1. 事件处理 1.1. 绑定事件方式 (1)行内绑定 语法: //最常用的使用方 ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- go——beego的数据库增删改查
一直都不理解使用go语言的时候,为什么还要自己去装beego,以为使用go便可以解决所有的问题,结果在朋友的点拨下,才意识到: go与beego的关系就好比是nodejs与thinkjs的关系,因此也 ...
- protobuf 安装 及 小测试
参考:http://shift-alt-ctrl.iteye.com/blog/2210885 版本: 2.5.0 百度云盘上有jar包. mac 上安装: 新建:/Users/zj/software ...