Home > Article > Backend Development > Files implemented in PHP are directly output and downloaded_PHP tutorial
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 if the FORM is completely filled out...
if ($form_completed) {
Header("Location: http://www.etoow.com/download/info_check.exe");
exit;
}
?>
Or the following situation:
" Start downloading file "
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. The procedure 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 file
//Input file tag
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");