PHP file download (hide the real download address)

Source: Internet
Author: User
Tags php download
PHP download files (hide real files) Sometimes we need to hide real files to prevent malicious downloads. we can use the following method (refer to the network, please inform us of infringement)


Reference content is as follows:

$ File_name = "example.exe ";

$ File_dir = "/public/www/download /";

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;

}


If the file is a remote file, the original code will be slightly changed. The program is as follows:


Reference content is as follows:

$ File_name = "example.exe ";

$ File_dir = "http://back.zhizhi123.com /";

$ File = @ fopen ($ file_dir. $ file_name, "r ");

If (! $ File ){

Echo "file not found ";

} Else {

Header ("Content-type: application/octet-stream ");

Header ("Content-Disposition: attachment; filename =". $ file_name );

While (! Feof ($ file )){

Echo fread ($ file, 50000 );

}

Fclose ($ file );

}

In this way, you can use PHP to directly output files.

Related Article

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.