Home Backend Development PHP Tutorial 请问一上文件上载时,HTTP响应消息头的有关问题

请问一上文件上载时,HTTP响应消息头的有关问题

Jun 13, 2016 pm 01:20 PM
content header length quot

请教一下文件下载时,HTTP响应消息头的问题。
我在学习利用PHP实现文件下载的时候,教材说需要提供三个http消息头:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
    header("Content-type: application/octet-stream");          //高速浏览器传递的是文件流
    header("Accept-Length: 2048");                             //文件大小
    header("Content-Disposition: attachment; filename=abc.txt"); //指定文件名

Copy after login


其中,“Content-type”和“Content-Disposition”两个消息头,我还是有点不太理解,所以我分别做了两个例子:

a.php:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php header("Content-type: application/octet-stream");
    header("Accept-Length: 2048");    

    echo 123;

Copy after login


b.php:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php header("Accept-Length: 2048");
    header("Content-Disposition: attachment; filename=abc.txt");    

    echo 123;

Copy after login


我的问题是:
1、这两个页面都少一个消息头,为什么还都能成功的下载文件?
2、我虽然指定的文件大小是2048个字节,但是真实输出的只有“123”三个字节,为什么也能正常的下载文件?
“Accept-Length”消息头是可设可不设吗?

我刚刚学习http,麻烦大家就我问的问题回答,不要引出更深的概念,因为我看不懂。。
十分感谢!:)

------解决方案--------------------
1、这两个页面都少一个消息头,为什么还都能成功的下载文件?

因为你没有设定Content-Length,默认连接结束时的输出就是下载内容。

2、我虽然指定的文件大小是2048个字节,但是真实输出的只有“123”三个字节,为什么也能正常的下载文件?“Accept-Length”消息头是可设可不设吗?

Accept-Length的意思是最大可接受字符串的长度。所以可设可不设。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does SpringBoot pass parameters in the Header through Feign calls? How does SpringBoot pass parameters in the Header through Feign calls? May 16, 2023 pm 08:38 PM

[SpringBoot] Passing parameters in the Header through Feign calls How to pass Header parameters through Feign Problem description When we use Feign to request the Api interface of another service in Spring Cloud, there is a need to pass the parameters in the Header. If no special processing is done, it will The parameters in the Header will be lost. Solution 1: Pass it through @RequestHeader(name="headerName"). For example: Feign is defined as follows @FeignClient(name="service-name")pub

What does linux header mean? What does linux header mean? Jul 18, 2023 pm 03:34 PM

The Linux header refers to the beginning of a file or data stream, which is used to contain metadata about the content. By correctly writing and using Header files, developers can better utilize system resources and improve code readability and Maintainability.

How to use PHP header() method to adjust web pages How to use PHP header() method to adjust web pages Mar 28, 2023 pm 01:54 PM

PHP is a powerful programming language that can be used to create dynamic websites and web applications. One of the most powerful features is PHP’s header() method. In this article, we will explore how to use PHP’s header() method to adjust web pages.

How to jump in php header How to jump in php header Dec 02, 2022 am 09:14 AM

How to implement jump in php header: 1. Use "Header("Location:$url");" syntax to implement jump; 2. Use if judgment to implement jump, with jump statements such as "if($_COOKIE[" u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');".

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

What is the difference between html5 tag head and header? What is the difference between html5 tag head and header? Jan 17, 2022 am 11:10 AM

Differences: 1. The head tag is used to define the head of the document, which is a container for all head elements, and the header tag is used to define the header (introduction information) of the document; 2. All browsers support the head tag, and older versions of browsers None of the browsers support the header tag, and browsers such as IE9+ and above are required to support the header tag.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Complete list of PHP file download functions: analysis of file download examples of readfile, header, Content-Disposition and other functions Complete list of PHP file download functions: analysis of file download examples of readfile, header, Content-Disposition and other functions Nov 18, 2023 pm 03:26 PM

Complete list of PHP file download functions: File download example analysis of readfile, header, Content-Disposition and other functions. File download is one of the essential functions in Web applications, and PHP, as a widely used Web development language, provides many A function and method to implement file downloading. This article will introduce commonly used file download functions in PHP, including readfile, header, Content-Dispo

See all articles