首页 > 编程技术 > php

PHP 获取远程文件大小常用方法总结

发布时间:2016-11-25 16:47

php有很多方法可以获取远程文件大小的哦,最常用的就有fsockopen、file_get_contents、curl函数哦,下面我来给各位总结一下。

1、fsockopen

 代码如下 复制代码

<?php
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.1rn");
fputs($fp,"Host:$url[host]rnrn");
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.111cn.netnet/")
?>


2、使用file_get_contents()

 代码如下 复制代码

<?php
$file = file_get_contents($url);
echo strlen($file);
?>

3. 使用get_headers()

 代码如下 复制代码

<?php
$header_array = 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
)
 


$size = $header_array['Content-Length'];
echo $size;
?>

4.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;
}

文章详细介绍PHP内置的Math函数的些使用过程的问题,希望些方法对各位有所帮助哦。

如题所示。。。木有做大规模的运算,还不知道。。。擦,PHP的Math函数运算原来是如此之慢的。。。大家还是麻烦点,手写多几句算鸟~~~

小二!上代码。。。。

 

 代码如下 复制代码
$start = microtime(TRUE);  
for ($i=0; $i < 200000; $i++)   {  
    $s = 0;       for ($j=0; $j < 3; $j++)       {   
       $s += ($j+$i+1) * ($j+$i+1);       }  
}  
echo microtime(TRUE) – $start;   # output: 0.33167719841003 

 

再对比下用Math函数的代码和结果

 代码如下 复制代码
$start = microtime(TRUE);  
for ($i=0; $i < 200000; $i++)   {  
    $s = 0;       for ($j=0; $j < 3; $j++)       {   
       $s += pow($j+$i+1, 2);       }  
}  
echo microtime(TRUE) – $start;   # output: 0.87528896331787 

 

看到木有,效率提升100%丫。。。以前还一直都认为是PHP内置的Math快,真是不测不知道。。像取绝对值abs,最大值max,最小值min 等 效率都不如原生的 if判断来得快~~

总的来说,php运算的确是很慢~~ 真心不适合做大规模的算法运算~~

在php中要获取或目录权限我们可使用fileperms函数来获取,fileperms() 函数返回文件或目录的权限,。若成功,则返回文件的访问权限。若失败,则返回 false。

例子 1

 代码如下 复制代码
<?php
echo fileperms("test.txt");
?>

输出:

33206

例子2

 代码如下 复制代码

/*
 * substr 返回字符串的子串
 * base_convert  在任意进制之间转换数字
 * fileperms  取得文件的权限
 */
// 获取权限
function getChmod($filepath){
    return substr(base_convert(@fileperms($filepath),10,8),-4);
}

本文章来介绍一个简单的文件操作函数fopen,fopen函数可以打开,读取,并且协助文件保存,下面我来给大家总结一下php文件的操作。

打开文件

fopen 最简单语法如下:

fopen(filepath,mode)

下面是打开一个文件的 PHP 代码示例:

 代码如下 复制代码

<?php

$f = fopen("c:\data\info.txt", "r");

?>

其中,c:\data\info.txt 是文件路径,r 表示打开文件的模式(mode) 为只读 (read only) 模式。

 

讲述使用 PHP 内置函数 fclose 关闭一个文件。

fclose 函数语法如下:

fclose(filepointer)

如果成功,fclose 函数返回 TRUE,如果失败,fclose 函数返回 FALSE。

下面是一个 fclose 函数的 PHP 代码示例:

 代码如下 复制代码

<?php

$f = fopen("c:\data\info.txt", "r");
fclose($f);
?>

fwrite 写入文件


fwrite 函数。

PHP 内置函数 fwrite 用于写入文件。

fwrite 函数的常用语法为:

fwrite(handle,string)
其中,参数 handle 表示文件指针资源 (通常由 fopen 函数创建),string 表示要写入的内容。

下面一个PHP 代码示例演示如何创建一个新文件,并写入内容,然后保存并关闭文件:

 代码如下 复制代码

<html>
<body>
<?php
$f= fopen("C:\blabla\php\write.txt","w");
fwrite($f,"It is awesome.");
fclose($f);echo "done";
?>
</body>
</html>

执行该 PHP 文件后,会创建一个路径为 C:blablaphpwrite.txt 的文件,文件的内容是It is awesome.。

如果你想在现有文件上再追加内容,你只要修改 fopen 的 参数 mode 值即可,如下:

 代码如下 复制代码

$f= fopen("C:\blabla\php\write.txt","a");

有关 fopen 函数的参数 mode 值,详见 fopen。

fwrite 函数返回写入文件的字节数 (number of bytes) ,如果出错,返回 FALSE。


fgets 读取文件一行内容


用 PHP 内置函数 fgets 可以读取文件的一行内容。

fgets读取文件一行内容的语法是:

fgets(filepointer)

下面我们举个例子讲述如何一行行读取一个文件。

假设我们有一个 sites.txt 文件,该文件有三行,内容如下:

woyouxian.comblabla.cngoogle.com
sites.txt 的文件路径是:

C:blablaphpsites.txt
我们用 PHP 一行行读取文件内容,PHP代码如下:

 代码如下 复制代码
<html><body><?php
$f= fopen("C:\blabla\php\sites.txt","r");
while (!feof($f))

$line = fgets($f); 
echo "site: ",$line,"<br />";
}fclose($f);
?>
</body></html>

执行该 PHP 文件,返回的显示结果是:

site: woyouxian.comsite: blabla.cnsite: google.com

该 PHP 代码的第一行是打开文件,最后一行是关闭一个文件。当中的 while 循环语句表示,当文件没有结束,就读取一行,循环执行,直到文件指针到文章末尾为止。

feof 函数是PHP 的一个内置函数,用来测试文件指针是否已经到了文件末尾。如果是返回 TRUE,如果不是,返回 FALSE。eof 的英文意思就是 end of file,很容易记。

正常情况下,fgets 函数的返回值是一个字符串,如果出错,返回 FALSE

implode()昨天就是把数组转换成一个字符串了,主要使用与在于此了,但是他转换过来的字符串会有一定的规则的,如果中间有分切符的,下面我来介绍implode()函数用法。

官方法介绍

implode(separator,array)

implode( )函数返回一个字符串的内容阵列。 implode — Join array elements with a string

 代码如下 复制代码


<?php
$arr = array('您好','百度!','美丽','一日!');echo implode(" ",$arr);
?>

输出.

您好 百度! 美丽 一日!


这样我就看出一implode用法了,它其实就这么简单哦。

标签:[!--infotagslink--]

您可能感兴趣的文章: