You will definitely laugh at me as simple as "downloading files"? Of course it's not as simple as imagined. For example, if you want the customer to fill out a form before downloading a certain file, your first idea must be to use the "Redirect" method, first check whether the form has been filled out and complete, and then point the URL to the file so that the customer can download it. However, if you want to build an e-commerce website about "online shopping", consider security issues, and you don't want the user to copy the URL to download the file directly. 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 the file
// Enter 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 path is "http" or "ftp" URL, the source code will change slightly, and the program is as follows:
$file_name = "info_check.exe";
$file_dir = "/";
$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 way you can use PHP to output files directly.
$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 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 path is "http" or "ftp" URL, the source code will change slightly, and the program is as follows:
$file_name = "info_check.exe";
$file_dir = "/";
$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 way you can use PHP to output files directly.