我的 PHP 中的文件传输代码正确吗?

发布于 2024-11-18 05:36:41 字数 1952 浏览 7 评论 0原文

我有这个 页面 应该是下载一首歌曲。下载对我来说在 Firefox 中有效,但在 chrome 和 safari 中没有任何反应。这是我的代码,

    public function download() {
    if (isset($this->request->get['order_download_id'])) {
        $order_download_id = $this->request->get['order_download_id'];
    } else {
        $order_download_id = 0;
    }
    $download_info = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_download od LEFT JOIN `" . DB_PREFIX . "order` o ON (od.order_id = o.order_id) WHERE o.customer_id = '" . (int)$this->customer->getId(). "' AND o.order_status_id > '0' AND o.order_status_id = '" . (int)$this->config->get('config_download_status') . "' AND od.order_download_id = '" . (int)$order_download_id . "'");

    if ($download_info->row) {
        $file = DIR_DOWNLOAD . $download_info->row['filename'];
        $mask = basename($download_info->row['mask']);
        $mime = 'application/octet-stream';
        $encoding = 'binary';

        if (!headers_sent()) {
            if (file_exists($file)) {
                header('Pragma: public');
                header('Expires: 0');
                header('Content-Description: File Transfer');
                header('Content-Type: ' . $mime);
                header('Content-Transfer-Encoding: ' . $encoding);
                header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
                header('Content-Length: ' . filesize($file));
                $file = readfile($file, 'rb');
                print($file);
            } else {
                exit('Error: Could not find file ' . $file . '!');
            }
        } else {
            exit('Error: Headers already sent out!');
        }
    }
}

我尝试了各种不同的方法来使其工作,但在两个浏览器中没有发生任何事情...任何想法或帮助都将是赞赏...

I have this page that is supposed to be a download for a song. The download works in firefox for me but in chrome and safari nothing happens..here is my code

    public function download() {
    if (isset($this->request->get['order_download_id'])) {
        $order_download_id = $this->request->get['order_download_id'];
    } else {
        $order_download_id = 0;
    }
    $download_info = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_download od LEFT JOIN `" . DB_PREFIX . "order` o ON (od.order_id = o.order_id) WHERE o.customer_id = '" . (int)$this->customer->getId(). "' AND o.order_status_id > '0' AND o.order_status_id = '" . (int)$this->config->get('config_download_status') . "' AND od.order_download_id = '" . (int)$order_download_id . "'");

    if ($download_info->row) {
        $file = DIR_DOWNLOAD . $download_info->row['filename'];
        $mask = basename($download_info->row['mask']);
        $mime = 'application/octet-stream';
        $encoding = 'binary';

        if (!headers_sent()) {
            if (file_exists($file)) {
                header('Pragma: public');
                header('Expires: 0');
                header('Content-Description: File Transfer');
                header('Content-Type: ' . $mime);
                header('Content-Transfer-Encoding: ' . $encoding);
                header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
                header('Content-Length: ' . filesize($file));
                $file = readfile($file, 'rb');
                print($file);
            } else {
                exit('Error: Could not find file ' . $file . '!');
            }
        } else {
            exit('Error: Headers already sent out!');
        }
    }
}

I have tried all kinds of different things to get this to work but nothing is happening in the two browsers...any ideas or help will be appreciated...

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

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

发布评论

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

评论(2

煮茶煮酒煮时光 2024-11-25 05:36:41

readfile 返回发送的字节数,不需要打印出来。您应该删除行 print($file);。否则,您发送的字节数将多于 Content-Length 标头指定的字节数,这将导致某些 HTTP 客户端丢弃您的答案。

另外,请考虑奇怪的文件名,例如

"\r\nLocation: http://evil.com\r\n\r\n<script>alert('XSS');</script>

您是否正确处理该文件名?

readfile returns the number of bytes sent, and needs not to be printed out. You should remove the line print($file);. Otherwise, you'll send more bytes than the Content-Length header specifies, and that will lead some HTTP clients to discard your answer.

Also, consider strange file names such as

"\r\nLocation: http://evil.com\r\n\r\n<script>alert('XSS');</script>

Are you handling that correctly?

中二柚 2024-11-25 05:36:41

查看附近的语法

header('Content-Disposition: attachment; filename="'.$file_name_with_space. '"');

,或者它可以是

header("Content-Disposition: attachment; filename='".$file_name_with_space."'" );

这里的游戏仅用引号括起来,如果编写正确,它会被视为字符串的一部分,否则会崩溃。

它适用于所有浏览器。 IE、FF、Chrome、SAFARI 我亲自检查过,所以继续。

See your syntax near

header('Content-Disposition: attachment; filename="'.$file_name_with_space. '"');

OR it can be

header("Content-Disposition: attachment; filename='".$file_name_with_space."'" );

Here the game is in Quotes only it will be treated as part of the string if it is written properly else will crash.

It works in all browser. IE, FF, Chrome, SAFARI I checked it personally so goahead.

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