curl 的远程文件大小

Posted

技术标签:

【中文标题】curl 的远程文件大小【英文标题】:Remote file size with curl 【发布时间】:2012-01-30 13:37:59 【问题描述】:

问题检查远程网址的文件大小

以下内容的本质是一个链接,我们可以在该链接上获得另一个文件链接

让我们说... 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));

【问题讨论】:

相关:How to get the file size of a remotely stored image? (php) 【参考方案1】:

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

这仅在远程主机提供有效的内容标头时才有效,即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;
?>

【讨论】:

嗨,伙计,poiskm.ru/index.php/get/strack/679ca5/2dc1d0/f?download.mp3 url 状态 302 重定向。内容长度不起作用。 musicbox.uz/size.php HTTP 状态:302 内容长度:未知 poiskm.ru/index.php/get/strack/679ca5/2dc1d0/f?download.mp3 检查。 再次。我已经回答了你的问题。引用:This will only work if the remote host is supplying valid content header, namely Content-Length. You cannot otherwise get file size without actually downloading it first..

以上是关于curl 的远程文件大小的主要内容,如果未能解决你的问题,请参考以下文章

php curl get 下载远程zip文件保存在本地例子

检查外部文件的大小,php

php恢复ftp上传与curl multi

php使用curl下载指定大小的文件

PHP 使用curl以多种格式获取文件大小的函数。

php使用curl下载指定大小的文件