【问题标题】:Download Image link using php使用php下载图片链接
【发布时间】:2011-12-06 21:00:59
【问题描述】:

我下载了这段代码用作下载按钮。

<?    
    $filename = $_GET["filename"];
    $buffer = file_get_contents($filename);

    /* Force download dialog... */
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    /* Don't allow caching... */
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    /* Set data type, size and filename */
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . strlen($buffer));
    header("Content-Disposition: attachment; filename=$filename");

    /* Send our file... */
    echo $buffer; 
?> 

问题是,文件名以文件名中的整个路径结尾,例如,这段代码:

<a href="download.php?filename=images/something.jpg">

以名为“images_something.jpg”的图像结束

我想从最终文件名中删除“images_”,到目前为止我还没有运气。

感谢您的帮助!

【问题讨论】:

  • 使用该脚本,任何人都可以从您的服务器下载任何文件,所以我会在使用它之前考虑保护它。
  • 我不知道,你能给我指一个网站,在那里我可以研究如何保护它吗?

标签: php image download


【解决方案1】:

如果你需要没有文件夹名的文件名部分,你必须使用 basename($filename) http://php.net/manual/en/function.basename.php

【讨论】:

    【解决方案2】:

    basename()

    $filename = basename($path);
    

    附言

    多次设置 Content-Type 可能不是强制下载的最佳方式。另外,我希望您在使用 file_get_contents 之前清理了 $filename 参数。

    pps

    使用readfile,不要缓存在内存中。

    【讨论】:

      【解决方案3】:
      $filename = basename($filename); 
      header("Content-Disposition: attachment; filename=$filename");
      

      将您的文件名设置为仅基本名称?

      除非您更改变量,否则不要在顶部执行此操作,这样您的路径仍然有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-11
        • 2018-06-26
        • 2012-08-27
        • 2015-09-29
        • 2011-01-25
        • 2011-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多