PHP How to implement a variety of parts download _php tutorial

Source: Internet
Author: User
Tags php language rar
If we use only download or JS window.location = ' file path/filename ' and the file type is browser-aware, then the browser will open the file directly instead of the popup download box to download the file, such as '. txt ', '. conf ', '. bin ' file, etc. Of course JS Document.execcommand (' SaveAs ', ' Mycodes.txt ') (the second parameter is to be saved as the file name) can also be downloaded, but its compatibility is not good, Firefox invalid. If your environment supports the PHP language, then you can use the header () to implement the file download:

Let's look at a simple example:

There are two files in the same directory index.php,test.php with adam.txt for downloading files:

test.php File Contents:

$filename = $filename = ' Adam.txt ';
echo "Download";

?>

index.php File Contents:

$filename = $_request[' filename '];

Header ("Content-type:text/plain");
Header (' content-disposition:attachment;filename= '. $filename);
Header (' content-transfer-encodeing:binary ');
ReadFile ($filename);

?>

When you open test.php and click Download, the download of the Adam.txt file will be implemented.

In the same vein, we can also implement the download of files such as. Jpg,.zip,.rar,.pdf, and then simply modify the header ("Content-type:text/plain") in the index.php, as shown below

Header ("Content-type:application/zip");//zip or rar

Header ("Content-type:application/pdf");//pdf

Header ("Content-type:image/jpeg");//Picture

Header ("Content-type:audio/mpeg");


Excerpted from Adamboy

http://www.bkjia.com/PHPjc/478339.html www.bkjia.com true http://www.bkjia.com/PHPjc/478339.html techarticle If we only use a href= file path/filename to download/A or JS window.location = file path/filename, and the file type is the browser can recognize, then the browser will be directly hit ...

  • 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.