2014-02-27 117 views
0

我想用php下載一個視頻/音頻文件格式的遠程URL。我曾嘗試過以下 代碼,其工作正常。但下載需要時間。如何以最快的方式下載。而且我還想獲​​得下載的文件大小和文件的持續時間。請幫助我如何做到這一點。如何使用PHP從任何遠程URL下載視頻或音頻文件?

我的代碼在這裏:

<?php 
$remote_url = ; // Any remote url with direct file path 
$content = get_headers($remote_url,1); 
$content = array_change_key_case($content, CASE_LOWER); 
if ($content['content-disposition']) { 
$tmp_name = explode('=', $content['content-disposition']); 
if ($tmp_name[1]) 
$realfilename = trim($tmp_name[1],'";\''); 
} else 
{ 
$stripped_url = preg_replace('/\\?.*/', '', $remote_url); 
$realfilename = basename($stripped_url); 
} 
$remote_file_contents = file_get_contents($remote_url); 
$local_file_path = $realfilename.''; 
$song_copied = file_put_contents($local_file_path, $remote_file_contents); 
?> 

感謝&問候。

回答

0

試試下面的代碼:

// We'll be outputting a video 
header('Content-type: video/flv'); 

header("Content-Disposition:attachment;filename=\"$psp\""); 
//allways a good idea to let the browser know how much data to expect 

header("Content-length: " . filesize($psp) . "\n\n"); 

echo file_get_contents($psp); //$psp should contain the full path to the video 

參見:how to download the video using php script