PHP file download class code, PHP file download processing class
Release: 2016-07-25 08:51:27
Original
1170 people have browsed it
-
-
/* - * Function: File download
- */
- /**
- * Usage: $download = new Download();
- * //Set parameters
- * $download->set_file_dir("c:/");
- * $download->set_file_name("boot.ini") ;
- * $download->set_read_bytes(1024);
- * //File download processing operation
- * $download->deal_with();
- * //Determine whether the file exists
- * if($download->is_file_exist ()){
- *echo "file_exist";
- *}else{
- *echo "file_not_exist";
- *}
- */
- class Download {
- private $file = null;//File handle
- private $file_dir = "";//The directory where the file is located
- private $file_name = "";//File name
- private $file_exist = false;//Indicates whether the file exists, the default is not
- private $read_bytes = 0;//Number of bytes read from the file
- private $mode = "r";//Access type of the file
public function __construct(){
- }
- < ;p>public function __destruct(){
- if(null != $this->file){
- fclose($this->file);
- }
- }
/* *
- * File download processing operations
- */ bbs.it-home.org
- public function deal_with(){
- //Full file path
- $file_path = $this->file_dir . $this->file_name;
-
//Check if the file exists
- if (file_exists($file_path)) {
- $this->file_exist = true;
//Open the file
- $this-> file = fopen($file_path, $this->mode);
// Input file tag
- Header("Content-type: application/octet-stream");
- Header(" Accept-Ranges: bytes");
- Header("Accept-Length: " . filesize($file_path));
- Header("Content-Disposition: attachment; filename=" . $this->file_name); p>
//Output file content
- while(!feof($this->file))
- {
- $out = fread($this->file, $this->read_bytes);
- if(!get_magic_quotes_gpc())
- {
- echo $out;
- }
- else
- {
- echo stripslashes($out);
- }
- }
- //echo fread($file, filesize($file_dir . $file_name)) ;
- }
- }
/**
- * The return value is true or false, and the value is used to determine whether the file exists
- */
- public function is_file_exist(){
- return $this->file_exist;
- }
* Parameter type is string - */
- public function set_file_dir($file_dir=""){
- $this->file_dir = $file_dir;
- }
/**
- * Parameter type is string
- */
- public function set_file_name($file_name=""){
- $this->file_name = $file_name;
- }
/**
- * The parameter type is integer
- */
- public function set_read_bytes( $read_bytes=1024){
- $this->read_bytes = $read_bytes;
- }
- }
- ?>
-
Copy code
|
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31