沃梦达 / 编程问答 / php问题 / 正文

带有 302 重定向的内容处理

Content-Disposition with 302 redirect(带有 302 重定向的内容处理)

本文介绍了带有 302 重定向的内容处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚这还有效,但我一定是不小心更改了一些东西,因为现在不行了.

This was working last night, but I must have accidentally changed something, because it isn't now.

从这些标题中应该清楚我想要做什么:

What I am trying to do should be clear from these headers:

Content-Disposition: attachment;filename=english_customizable.xml
Location: http://tortoisewrath.com/files/2.xml

但是,当发送此标头时,Content-Disposition 部分在重定向后不起作用.

However, when this header is sent, the Content-Disposition part doesn't work after the redirect.

...为什么?

推荐答案

你试图做的是不明智的检查这个问题;标题位置 + 内容配置

What you're trying to do is inadvisable check this question; Header Location + Content Disposition

内容配置 + 位置标题

但是你可以做到,为了让它工作,你必须在发送之前缓冲你的整个响应.你可以用输出缓冲来做到这一点

But you can do it, to make it work you will have to buffer your whole response before sending it. You can do this with output buffering

  • http://php.net/manual/en/book.outcontrol.php

否则浏览器可能会在下载文件之前解释 Location 标头.无论哪种方式都很粗略,所以你不应该想要这样做.

Else the browser may interpret the Location header before the file is downloaded. It's sketchy either way, so you shouldn't want to do this.

请注意使用Content-Disposition:attachment;强制另存为"将确保客户端不会去/导航任何地方,因此下面的方法无论如何,它自己的应该没问题.

Please note that forcing 'save as' using Content-Disposition: attachment; will make sure the client doesn't go/navigate anywhere, so the method below on its own should be fine in any case.

在 php 中流式传输文件

引用一个头脑正确的人:

// To use header() with 'content-type', why don't you use mime_content_type() function rather than checking the type on the basis of extension? 
// Example code: 

<?php 
$file="test.docx"; 
header("Pragma: public"); 
header('Content-disposition: attachment; filename='.$file); 
header("Content-type: ".mime_content_type($file)); 
header('Content-Encoding: identity'); 
ob_clean(); 
flush(); 
readfile($file); 
?> 

// Use $file to map to whichever type of file. 
// Note: the mime types should already be defined in apache settings

来源:http://www.php.net/manual/en/function.header.php#107581

请注意,原始答案使用了 Content-Transfer-Encoding,它实际上并不存在于 HTTP 中.该来源下方的评论对此进行了解释:http://www.php.net/manual/en/function.header.php#107044

Note that the original answer used Content-Transfer-Encoding which doesn't actually exist in HTTP. The comment below that source explains it: http://www.php.net/manual/en/function.header.php#107044

这篇关于带有 302 重定向的内容处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有 302 重定向的内容处理

基础教程推荐