2014-02-27 93 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