php - 使用 curl 的远程文件大小

标签 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));

最佳答案

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

这仅在远程主机提供有效内容 header (即 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;
?>

关于php - 使用 curl 的远程文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9064733/

相关文章:

php - Chrome 上未捕获的 ReferenceError : tinyMCE is not defined , joomla 2.5.7 错误

php - 在php中添加两个时间值

asp.net - 我可以使用附加参数增加 MVC Controller 操作的 ASP.NET 请求的 maxRequestLength 吗?

c++ - 文件大小方法 boost 库奇怪

php - 仅使用第一个字母获取列中的多个单词

php - 如何将产品从 Magento 导出到 Woocommerce?

ssl - Traefik TLS 证书导致 curl 出现 "unknown CA"错误,适用于浏览器

php - 使用 PHP 的 FedEx Shipping API

python - 如何在python中使用urllib2获取身份验证 token ?

javascript - AWS S3 CORS上传-文件大小控制