为什么我的 openfile.php 在 Safari 或 IE 中无法正常工作?

发布于 2024-12-23 09:03:23 字数 1648 浏览 3 评论 0原文

我有一个非常简单的文件,名为 openfile.php,它设置内容处置并强制浏览器下载指定的文件。

它在 FF 中工作正常,但在 Safari 中,它会下载一个奇怪的 .xhtml 文件,经过仔细检查,该文件似乎是会出现的默认错误页面。

在 IE 中,您只会被定向到此错误页面。

谁能推断出这里发生了什么?

这是一个链接:http://hqinternetsolutions.com/Websites/Fabric%20Traditions/? page_id=215

这是打开文件的代码

<?php
if ( ! isset($_GET['file']) )
    die();

if ( strpos( $_GET['file'], (isset($_SERVER['HTTPS']) ? 'https|' : 'http|') . $_SERVER['SERVER_NAME'] ) === false )
    die();

require_once('../lib/class.mimetype.php');
$mime = new mimetype();

$fPath = str_replace('http|', 'http://', $_GET['file']);
$fPath = str_replace('https|', 'https://', $fPath);
$fType = $mime->getType( $fPath );
$fName = basename($fPath);

$origname = preg_replace('/_#_#\d*/','',$fName);

$fContent = fetch_content( $fPath );

output_content( $fContent, $origname );

function fetch_content( $url ) {
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_HEADER, 0 );

    ob_start();

    curl_exec( $ch );
    curl_close( $ch );

    $fContent = ob_get_contents();

    ob_end_clean();

    return $fContent;
}

function output_content( $content, $name ) {
    header( "Expires: Wed, 9 Nov 1983 05:00:00 GMT" );
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header( "Content-Disposition: attachment; filename=" . $name );
    header( "Content-type: application/octet-stream" );
    header( "Content-Transfer-Encoding: binary" );

    echo $content;
}
?>

I have a very simple file called openfile.php that sets a content-disposition and forces a browser to download a specified file.

It works fine in FF, but in Safari, it downloads a strange .xhtml file that on closer examination, appears to be the default error page that would occur.

In IE, you are just directed to this error page.

Can anyone deduce what is going on here?

Here is a link : http://hqinternetsolutions.com/Websites/Fabric%20Traditions/?page_id=215

This is the code that opens the file

<?php
if ( ! isset($_GET['file']) )
    die();

if ( strpos( $_GET['file'], (isset($_SERVER['HTTPS']) ? 'https|' : 'http|') . $_SERVER['SERVER_NAME'] ) === false )
    die();

require_once('../lib/class.mimetype.php');
$mime = new mimetype();

$fPath = str_replace('http|', 'http://', $_GET['file']);
$fPath = str_replace('https|', 'https://', $fPath);
$fType = $mime->getType( $fPath );
$fName = basename($fPath);

$origname = preg_replace('/_#_#\d*/','',$fName);

$fContent = fetch_content( $fPath );

output_content( $fContent, $origname );

function fetch_content( $url ) {
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_HEADER, 0 );

    ob_start();

    curl_exec( $ch );
    curl_close( $ch );

    $fContent = ob_get_contents();

    ob_end_clean();

    return $fContent;
}

function output_content( $content, $name ) {
    header( "Expires: Wed, 9 Nov 1983 05:00:00 GMT" );
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header( "Content-Disposition: attachment; filename=" . $name );
    header( "Content-type: application/octet-stream" );
    header( "Content-Transfer-Encoding: binary" );

    echo $content;
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分開簡單 2024-12-30 09:03:23

你的标题是

Content-Disposition: attachment; filename=Pillow1.pdf

,但应该是

Content-Disposition: attachment; filename="Pillow1.pdf"

不确定这是否是问题,因为我不使用Windows。

Your header is

Content-Disposition: attachment; filename=Pillow1.pdf

but it should be

Content-Disposition: attachment; filename="Pillow1.pdf"

Not sure if this is the problem, as I don't use windows.

¢蛋碎的人ぎ生 2024-12-30 09:03:23

即使在 Firefox 中,该链接也对我不起作用,下载的内容是一些 HTML 页面。

查看您发送的标头 (http://redbot.org/?uri=http%3A%2F%2Fhqinternetsolutions.com%2FWebsites%2FFabric%2520Traditions%2Fwp-content%2Fplugins%2Fwp-publication-archi ve%2Fincludes%2Fopenfile.php%3Ffile%3Dhttp|hqinternetsolutions.com%2FWebsites%2FFabric%2520Traditions%2Fwp-content%2Fuploads%2F2011%2F12%2FPillow1.pdf)我注意到:

  • Expires 标头已损坏
  • ,HTTP 中没有 Content-Transfer-Encoding
  • Content-Type 应该是 PDF 的类型

(但这些都不能解释为什么发送错误的内容)

The link doesn't work for me even in Firefox, the content that get's downloaded is some HTML page.

Looking at the headers you are sending (http://redbot.org/?uri=http%3A%2F%2Fhqinternetsolutions.com%2FWebsites%2FFabric%2520Traditions%2Fwp-content%2Fplugins%2Fwp-publication-archive%2Fincludes%2Fopenfile.php%3Ffile%3Dhttp|hqinternetsolutions.com%2FWebsites%2FFabric%2520Traditions%2Fwp-content%2Fuploads%2F2011%2F12%2FPillow1.pdf) I note:

  • the Expires header is broken
  • there is no Content-Transfer-Encoding in HTTP
  • Content-Type should be the type for PDF

(but none of these explain why the wrong content is sent)

神仙妹妹 2024-12-30 09:03:23

该 PHP 无法运行的原因是 URL 中的 %20 。不能有空格,否则会导致脚本中的分隔符查找器失败。

The reason this PHP will not work is because of the %20 in the URL. You can't have spaces or it causes the delimiter finder in the script to fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文