PHP code to implement download function_PHP tutorial
Jul 21, 2016 pm 03:15 PM
wzskynet#163.com
·php escapeshellcmd multi-byte encoding vulnerability
·Detailed explanation of the application of caching technology in PHP
·Using PHP V5 to develop multi-task applications
·Detailed analysis of PHP sending to MySQL Data Process
·A brief discussion on the method of static publishing in PHP
You will definitely laugh at me. Is it worth saying that "downloading files" is so simple? Of course it's not as simple as you think. For example, if you want customers to fill out a form before they can download a certain file, your first idea must be to use the "Redirect" method. First check whether the form has been filled in and complete, and then point the URL to the file. , so that customers can download, for example, the following code written by the author:
<?
// Check whether the FORM is completely filled out...
if ($form_completed) {
Header("Location: http://www.jb51.net/download/info_check.exe");
exit;
}
?>
Or the following situation:
<a href="http://www.yourwebl.com/users/download.php?id=124524">Start downloading file</a>
Here, the ID method is used to receive the number of the file to be downloaded, and then the "Redirect" method is used to connect to the actual URL.
If you want to make an e-commerce website about "online shopping" and consider security issues, you don't want users to directly copy the URL to download the file. The author recommends that you use PHP to directly read the actual file and then download it. Do it. The program is as follows:
<?
$file_name = "info_check.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
// Enter the 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;}
?>
And if the file path is an "http" or "ftp" URL, the source code will change slightly. The procedure 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 search Less than ";
} 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 directly output the file using PHP.
Achieve safe downloading of php files!
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);
}
}

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
