PHP Simple Download

Source: Internet
Author: User
Tags explode fread ranges

PHP escapeshellcmd Multi-byte encoding vulnerability
• Detailed explanation of the application of the cache technology in PHP
• Development of multitasking applications with PHP V5
• Detailed parsing of PHP sending data to MySQL process
· On the method of static release of PHP
You're going to laugh at me. "Download file" So simple is worth saying? Of course it's not as simple as you think. For example, you want customers to fill out a form, you can download a file, your first thought must be "Redirect" method, first check whether the form has been completed and complete, and then the URL refers to the file, so that customers can download, such as the author of the following code:

Copy CodeThe code is as follows:
<?
Check if the form is complete ...
if ($form _completed) {
Header ("Location:http://www.jb51.net/download/info_check.exe");
Exit
}
?>


Or in the following cases:

Copy CodeThe code is as follows:
<a href= "http://www.yourwebl.com/users/download.php?id=124524" > Start download File </a>


The ID is used here to receive the number of the file to download, and then "Redirect" the way to connect to the actual URL.

If you want to do an e-commerce website on "online shopping", considering security issues, you do not want users to directly copy the URL to download the file, I suggest you use PHP directly read the actual file and then download the method to do. The procedure is as follows:

Copy CodeThe code is as follows:
<?
$file _name = "Info_check.exe";
$file _dir = "/public/www/download/";
if (!file_exists ($file _dir. $file _name)) {//check if file exists
echo "File not found";
Exit
} else {
$file = fopen ($file _dir. $file _name, "R"); Open File
Input File Label
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 contents
Echo fread ($file, FileSize ($file _dir. $file _name));
Fclose ($file);
Exit;}
?>


If the file path is "http" or "FTP" URL, then the source code will change a little, the program is as follows:

Copy CodeThe code is as follows:
?
$file _name = "Info_check.exe";
$file _dir = "http://www.jb51.net/";
$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);
}
? >


This allows you to output the file directly with PHP.

Implement PHP file security download!

Copy CodeThe code is as follows:
Public Function Downloads ($name) {
$name _tmp = Explode ("_", $name);
$type = $name _tmp[0];
$file _time = Explode (".", $name _tmp[3]);
$file _time = $file _time[0];
$file _date = Date ("Y/md", $file _time);
$file _dir = Site_path. " /data/uploads/$type/$file _date/";

if (!file_exists ($file _dir. $name)) {
Header ("content-type:text/html; Charset=utf-8 ");
echo "File not found!";
Exit
} else {
$file = fopen ($file _dir. $name, "R");
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:". FileSize ($file _dir. $name));
Header ("content-disposition:attachment; Filename= ". $name);
Echo fread ($file, FileSize ($file _dir. $name));
Fclose ($file);
}
}

PHP Simple Download

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.