2012-01-30 4 views
3

Проблемы проверить размер файл удаленного сервераУдаленного размера файла с завитком

Сущности ниже приводится ссылка на которую мы получаем другую ссылку на файл

Давайте говорить ... 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)); 
+0

Связанный: [Как получить размер файла удаленно сохраненного изображения? (php)] (http://stackoverflow.com/questions/3894653/how-to-get-the-file-size-of-a-remotely-stored-image-php) – hakre

ответ

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; 
?> 
+0

Привет, Гай, http: // poiskm .ru/index.php/get/strack/679ca5/2dc1d0/f? скачать.mp3 URL-адрес 302 перенаправления. не работает Content-Length. –

+0

Я уже ответил на ваш вопрос. – Gajus

+0

http://musicbox.uz/size.php HTTP Status: 302 Content-Length: неизвестно http://poiskm.ru/index.php/get/strack/679ca5/2dc1d0/f?download.mp3 check. –

Смежные вопросы