卷曲的远程文件大小

时间:2012-01-30 13:37:59

标签: php curl filesize

问题检查远程URL的文件大小

以下内容是我们获取文件的另一个链接的链接

让我们说...... http://poiskm.ru/index.php/get/strack/679ca5/2dc1d0/f?download.mp3

-

http://musicbox.uz/download.php?file=http://poiskm.ru/index.php/get/strack/679ca5/2dc1d0/f?download.mp3

帮助我!!

代码:

$file=$_GET['file'];

$ch = curl_init($file);
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) {}
$contentLength = '0';

if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

$url = $file;
$file_name = basename($url); 


//lets be nice to the user and replace the spaces with happy things
$file_name=str_replace("%20","_",$file_name);

//this is the filename we get to play with
$infile = $url;
$file_name = stristr ($infile,basename ($infile));


header( "Pragma: public" ); // required
header( "Expires: 0" );
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); // required for certain browsers 
header("Content-Type: application/mp3");
header('Content-Disposition: attachment; filename="[www.MusicBox.uz]'.urldecode($file_name).'"');
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$contentLength);
//header('Connection: close');
// The File source is in .mp3 originally
//This is the file that we are downloading
readfile(stripslashes($file));

1 个答案:

答案 0 :(得分:1)

Google上的第一个结果http://www.php.net/manual/en/function.filesize.php#92462。 SOF不是谷歌的替代品。

这仅在远程主机提供有效内容标头(即Content-Length)时才有效。如果没有先实际下载文件,你就无法获得文件大小。

<?php
$remoteFile = 'http://us.php.net/get/php-5.2.10.tar.bz2/from/this/mirror';
$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 (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>