Illustrator以PDF格式打开

时间:2009-12-31 09:13:06

标签: php file mime-types

我正在为我的办公室编写文件共享应用程序。我遇到的一个奇怪的问题是,当您点击下载按钮时,Illustrator文件将以PDF格式打开。

由于插图文件的mime类型为application/pdf,因此会触发此问题。因此浏览器在读取文件时会触发Acrobat打开文件。有什么方法可以指示浏览器在Illustrator中打开文件吗?

或者有没有办法在上传文件后修改mime类型?后端代码是PHP。

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

执行此操作的一种方法是强制浏览器显示“下载文件”-dialog。因此,用户可以决定如何处理该文件。

这可以通过PHP-Headers完成。 (http://www.php.net/manual/en/function.header.php#83384

还有一个关于如何做到这一点的例子(Post 83384):

<?php
    // downloading a file
    $filename = $_GET['path'];

    // fix for IE catching or PHP bug issue
    header("Pragma: public");
    header("Expires: 0"); // set expiration time
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    // browser must download file from server instead of cache

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

    // use the Content-Disposition header to supply a recommended filename and
    // force the browser to display the save dialog.
    header("Content-Disposition: attachment; filename=".basename($filename).";");

    /*
    The Content-transfer-encoding header should be binary, since the file will be read
    directly from the disk and the raw bytes passed to the downloading computer.
    The Content-length header is useful to set for downloads. The browser will be able     to
    show a progress meter as a file downloads. The content-lenght can be determines by
    filesize function returns the size of a file.
    */
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));

    @readfile($filename);
    exit(0);
?>

使用此示例时,请考虑使用

$filename = $_GET['path'];

是一个很大的安全问题。您应该使用ID之类的东西或验证输入。 例如:

if($_GET['file'] == 1) {
    $filename = foobar.pdf;
} elseif($_GET['file'] == 2) {
   $filename = foo.pdf;
} else {
   die();
}