Php uses the header to download the code-PHP source code

Source: Internet
Author: User
In php, if you want to download slice files, php files, and html files, we will open them directly if you click them on the website. If you want to download these files, you can use the phpheader function to perform operations, the following describes how to use the phpheader function to download files. For more information, see. In php, if you want to download slice files, php files, and html files, we will open them directly if you click them on the website. If you want to download these files, you can use the php header function, next, I will introduce the example of using the php header function to download files. For more information, see.

Script ec (2); script

Example 1

The Code is as follows:

Function download ($ file_url, $ new_name = ''){
If (! Isset ($ file_url) | trim ($ file_url) = ''){
Return '20140901 ';
}
If (! File_exists ($ file_url) {// check whether the file exists
Return '20140901 ';
}
$ File_name = basename ($ file_url );
$ File_type = explode ('.', $ file_url );
$ File_type = $ file_type [count ($ file_type)-1];
$ File_name = trim ($ new_name = '')? $ File_name: urlencode ($ new_name). '.'. $ file_type;
$ File_type = fopen ($ file_url, 'R'); // open the file
// Input file tag
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length:". filesize ($ file_url ));
Header ("Content-Disposition: attachment; filename =". $ file_name );
// Output file content
Echo fread ($ file_type, filesize ($ file_url ));
Fclose ($ file_type );
}

Example 2

The following php code can solve the problem:

The Code is as follows:
If (empty ($ _ GET ['filename']) | empty ($ _ GET ['filedir']) | empty ($ _ GET ['fileid']) {
Echo 'script alert ("illegal connection! "); Location. replace (" index. php ") script '; exit ();
}
$ File_name = $ _ GET ['filename'];
$ File_dir = $ _ GET ['filedir'];
$ FileId = $ _ GET ['fileid'];
$ File_dir = $ file_dir ."/";
If (! File_exists ($ file_dir. $ file_name) {// check whether the file exists
Echo "file not found ";
Exit;
} Else {
$ File = fopen ($ file_dir. $ file_name, "r"); // open the file
// Input file tag
Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length:". filesize ($ file_dir. $ file_name ));
Header ("Content-Disposition: attachment; filename =". $ file_name );
// Output file content
Echo fread ($ file, filesize ($ file_dir. $ file_name ));
Fclose ($ file );
Exit ();
}
?>

Direct File Download

The Code is as follows:
$ File = get_file_address (); // the actual address of the file (url is supported, but url is not recommended)

If (file_exists ($ file )){
Header ('content-Description: File Transfer ');
Header ('content-Type: application/octet-stream ');
Header ('content-Disposition: attachment; filename = '. basename ($ file ));
Header ('content-Transfer-Encoding: binary ');
Header ('expires: 0 ');
Header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0 ');
Header ('pragma: public ');
Header ('content-Length: '. filesize ($ file ));
Ob_clean ();
Flush ();
Readfile ($ file );
Exit;
}
?>

TIPS: the header function can not only download files, but also has many other functions, such as sending status information such as 404,301.

Note: before using the header, we must have no output before the header; otherwise, an error will be reported.

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.