Home > Backend Development > PHP Tutorial > PHP code to implement download function_PHP tutorial

PHP code to implement download function_PHP tutorial

WBOY
Release: 2016-07-21 15:15:29
Original
774 people have browsed it

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:

Copy code The code is as follows:

// 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:
Copy code The code is as follows:

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:
Copy code The code 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:
Copy code The 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 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!
Copy code The 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);
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326116.htmlTechArticlewzskynet#163.com ·php escapeshellcmd multi-byte encoding vulnerability·Detailed explanation of the application and utilization of caching technology in PHP Develop multi-tasking applications with PHP V5·Detailed analysis of PHP sending data to MySQL...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template