2011-04-16 31 views
0

我一直试图从一个URL获取文件的名称,我发现它很简单,基名 ,直到我来到没有真正名称的标志的URL,已下载如何从一个url获取下载名称php编码

这里是链接的一个例子,我发现

这里的实际名称为youtubedownloadersetup272.exe http://qdrive.net/index.php/page-file_share-choice-download_file-id_file-223658-ce-0

,你可以看到它没有显示名字,直到下载。

我一直在寻找很多,我绝望地找不到任何东西,如果有人能指出我的方式谢谢,生病的apreciate。

我很抱歉打扰,但我foun从download.com这个链接,我不瑟使用curl

<?php 

function getFilename($url){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_NOBODY, 1); 
    $data = curl_exec($ch); 

echo $data;  
preg_match("#filename=([^\n]+)#is", $data, $matches); 

    return $matches[1]; 
} 

echo getFilename("http://software-files-l.cnet.com/s/software/11/88/39/66/YouTubeDownloaderSetup272.exe?e=1302969716&h=89b64b6e8e7485eab1e560bbdf68281d&lop=link&ptype=1901&ontid=2071&siteId=4&edId=3&spi=fdc220b131cda22d9d3f715684d064ca&pid=11883966&psid=10647340&fileName=YouTubeDownloaderSetup272.exe"); 

?> 

它回声$的数据返回这个文件名

HTTP/1.1 200 OK 服务器:Apache 接受-范围:字节 内容处置:附件 内容类型:应用程序/下载 年龄:866 日期:星期六,2011年4月16日10时十六分54秒GMT 的Last-Modified:周五, 2011年4月8日18时04分41秒GMT 的Content-Length:4700823 连接:保持活跃

如果我理解你给我它不会工作,因为它没有文件名的纸条, 有没有办法让名称与外出不得不做正则表达式或解析网址(YouTubeDownloaderSetup272.exe?e .........),就像你给我的脚本?

回答

0

您需要使用curl或某个其他库来请求该文件并查看响应的标题。

你会找一个像头:

Content-Disposition: attachment; filename=??? 

其中问号是文件的名称。

(你仍然必须下载文件,或者至少看起来像你下载文件。)

+0

你可以做'HEAD'请求而不是'GET'来获取头文件而不是文件。 – Javier 2011-04-16 01:36:05

+0

@Javier是的,你可以。 – 2011-04-16 01:46:15

0
function getFilename($url){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_NOBODY, 1); 
    $data = curl_exec($ch); 

preg_match("#filename=([^\n]+)#is", $data, $matches); 

    return $matches[1]; 
} 

echo getFilename("http://qdrive.net/index.php/page-file_share-choice-download_file-id_file-223658-ce-0"); //YouTubeDownloaderSetup272.exe 

对于download.com ...

function download_com($url){ 

    $filename = explode("?", $url); 
    $filename = explode("/", $filename[0]); 
    $filename = end($filename); 

    return $filename; 

} 

echo download_com("http://software-files-l.cnet.com/s/software/11/88/39/66/YouTubeDownloaderSetup272.exe?e=1302969716&h=89b64b6e8e7485eab1e560bbdf68281d&lop=link&ptype=1901&ontid=2071&siteId=4&edId=3&spi=fdc220b131cda22d9d3f715684d064ca&pid=11883966&psid=10647340&fileName=YouTubeDownloaderSetup272.exe"); 
+0

你好,谢谢我正要告诉你,它不工作,但我看到你改变它。感谢所有的帮助 – marisoldelta 2011-04-16 09:59:44

+0

你能帮我从download.com – marisoldelta 2011-04-16 10:25:19

+0

这个新问题@marisoldelta,正则表达式是坏的,但现在它的作品,因为download.com你不需要这个,我会更新我的答案不久。 – 2011-04-16 11:36:23

相关问题