【问题标题】:how to obtain the download name from an url php coding如何从 url php 编码中获取下载名称
【发布时间】:2011-08-06 17:24:01
【问题描述】:

我一直在尝试从 url 获取文件的名称,我发现它很简单,基本名称 直到我来的网址没有真实姓名的迹象,直到下载

这是我找到的链接示例

这里的真名是 youtubedownloadersetup272.exe http://qdrive.net/index.php/page-file_share-choice-download_file-id_file-223658-ce-0

你可以看到它在下载之前没有显示名称。

我找了很多东西,一无所获,不胜感激,如果有人能指点我,谢谢。

很抱歉再次打扰,但我从 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"); 

?>

它用 echo $data 返回这个

HTTP/1.1 200 正常 服务器:阿帕奇 接受范围:字节 内容处置:附件 内容类型:应用程序/下载 年龄:866 日期:2011 年 4 月 16 日星期六 10:16:54 GMT 最后修改时间:格林威治标准时间 2011 年 4 月 8 日星期五 18:04:41 内容长度:4700823 连接:保持活动

如果我理解你给我的脚本它不会工作,因为它没有文件名, 有没有一种方法可以在不需要进行正则表达式或解析 url (YouTubeDownloaderSetup272.exe?e....) 的情况下获取名称,就像你给我的脚本一样?

【问题讨论】:

    标签: php url download filenames


    【解决方案1】:
    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
    

    下载.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");
    

    【讨论】:

    • 你好,谢谢,我正要告诉你它不起作用,但我看到你改变了它。感谢所有帮助
    • 你能帮我解决这个来自 download.com 的 URL 的新问题吗
    • @marisoldelta,正则表达式很糟糕,但现在它可以工作了,对于 download.com,你不需要这个,我会尽快更新我的答案。
    【解决方案2】:

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

    您将寻找如下标题:

    Content-Disposition: attachment; filename=???
    

    问号是文件名。

    (您仍然必须下载该文件,或者至少看起来您正在下载该文件。)

    【讨论】:

    • 您可以使用HEAD 请求而不是GET 来获取标头而不是文件。
    猜你喜欢
    • 2015-02-24
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多