2012-10-04 72 views
0

每个文件的大小,我知道卷曲会得到一个页面的总下载大小,但我期待的下载大小分为总图像下载,总脚本下载大小,总的样式尺寸下载等等等等卷曲 - 获取从最初的卷曲请求

是什么打算这样做这样的一般方式...我发现这个链接PHP: Remote file size without downloading file

,我想它有事情做与做一个for循环和卷曲得到大小由inital curl请求拉入的每个文件的内容。

让我知道如果任何人有任何提示!

感谢

回答

0

使该功能:

<?php 
    getResourceSize($remoteFile /*link of the file*/) 
    { 
     $ch = curl_init($remoteFile); 
     curl_setopt($ch, CURLOPT_NOBODY, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_HEADER, true); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here) 
     $data = curl_exec($ch); 
     curl_close($ch); 
     if ($data === false) { 
     echo 'cURL failed'; 
     exit; 
     } 

     $contentLength = 'unknown'; 
     $status = 'unknown'; 
     if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) { 
     $status = (int)$matches[1]; 
     if($status == ('404' || '500') return 'error'; 
     } 
     if (preg_match('/Content-Length: (\d+)/', $data, $matches)) { 
     $contentLength = (int)$matches[1]; 
     return $contentLength; 
     } 
    } 
?> 

现在初始化总下载量为:

$files = array 
[ 
    "http://...firstFile.ext", 
    "http://...secondFile.ext", 
    "http://...thirdFile.ext", 
    ... 
]; 

$totalDownloadSize = 0; 

foreach($file in $files) 
    $totalDownloadSize += GetResourceSize($file);