Php file downloads

Source: Internet
Author: User
Php implements file downloads. PHP implements file downloads using code. For more information, see PHP code. generally, URLs are called to download files, however, this method cannot be used when ie can recognize opened files, such as downloading an image or html webpage. in this case, programming is required. the following php code can solve this problem :? If (empty ($ _ GET [we generally call URLs for download, but this method cannot be used when ie can identify opened files, for example, to download an image or html webpage, you need to program it. the following php code can be used:
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 ();
}
?>

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.