【问题标题】:How to save correct filename through proxy page如何通过代理页面保存正确的文件名
【发布时间】:2013-02-05 07:22:47
【问题描述】:

我是新手。试图发展我关于 PHP 和 JavaScript 的知识。这一次,我已经敲了太多次头,并且搜索了足够长的时间。

我正在尝试创建一个代理页面来查看 PDF。有用!但是当我想保存文件时,它自己命名为“proxy.pdf”。在我的 Transformer Prime 平板电脑上,它会下载脚本:“proxy.php”。

这就是我的代理 PHP 代码的样子:

<?php

session_start;

// define path to image folder
$image_folder = realpath(dirname(__FILE__))."/imagefolder/";
// get image name from the query string
// no probe
if (isset($_GET['pic']) && basename($_GET['pic']) == $_GET['pic'])
{   
    $pic = $image_folder.$_GET['pic'];
    if (file_exists($pic) && is_readable($pic)) 
    {
        // get the filename extension
        $ext = substr($pic, -3);
        // set the MIME type
        switch ($ext)
        {
    case 'jpg':
    $mime = 'image/jpeg';
    break;
    case 'gif':
    $mime = 'image/gif';
    break;
    case 'png':
    $mime = 'image/png';
    break;
    case 'pdf':
    //alt octet-stream
    $mime = 'application/pdf';
    break;
    default:
    $mime = false;
    } 
    // if a valid MIME type exists, display the image 
    // by sending appropriate headers and streaming the file
    if ($mime)
    {

        header('Content-type: '.$mime);
        header('Content-length: '.filesize($pic));

        $file = fopen($pic, 'rb');
        if ($file)
        {
            fpassthru($file);
            exit;
        }
    }
}
}
 ?>

【问题讨论】:

    标签: proxy filenames


    【解决方案1】:

    当我对我的 ASP.NET 应用程序有同样的问题时,我偶然发现了这个问题。使用 Content-Disposition 标头对我来说是这样做的。我不会假装自己是 PHP 专家,但是如果您将此行添加到其他标题旁边,它应该可以完成工作。

    header('Content-Disposition: attachment; filename='.$_GET['pic']);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-01
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多