PHP uses header function to implement file download ____ function

Source: Internet
Author: User

File downloads are simple:

You can use hyperlinks in HTML to jump to a file for downloading:

<?php echo ' <a href =  "http://www.localhost/download/1.zip" > Download </a> ';? >
The use method is the user directly clicks the hyperlink in the browser.

But this download method has some problems: some browsers can directly identify the type of data, such as text, part of the image and so on. will be displayed directly in the browser rather than the download.

Therefore, this method is not usually used.

In PHP, you can use the header function to set the server's file as an attachment type to notify the customer of the download. Use the following:

    Header (' Content-type:application/zip ');
    Header (' content-disposition:attachment; filename= "1.zip");
    ReadFile (' 1.zip ');
It should be noted that when using header downloads, clients can only use the <a> tag request to the action in the controller on the server side (different from the above to the specific file).

Can I use Ajax? The answer is no.

Ajax sending requests is certainly not a problem, but the data retrieved by Ajax is a JSON string. So the file stream cannot be implemented.

Uploading files can still be Ajax. Because the data type that Ajax sends is not limited to JSON strings.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.