1. Functions based on downloading files on the Web
Public Function Putfile ($file _dir, $file _name) {$file _dir = Chop ($file _dir);//Remove extra spaces in the path//Get the path of the file to download if ($file _dir! = ") {$file _path = $file _dir; if (substr ($file _dir,strlen ($file _dir) -1,strlen ($file _dir))! = '/') $file _path. = '/'; $file _path. = $file _name; } else $file _path = $file _name; Determine if the file to be downloaded exists if (!file_exists ($file _path)) {//alert (' Sorry, the file you want to download does not exist '); return false; } $file _size = FileSize ($file _path); Header ("CONTENT-TYPE:APPLICATION/OCTET-STREAM;CHARSET=GBK"); Header ("Accept-ranges:bytes"); Header ("Accept-length: $file _size"); Header ("content-disposition:attachment; Filename= ". $file _name); $fp = fopen ($file _path, "R"); $buffer _size = 1024; $cur _pos = 0; while (!feof ($fp) && $file _size-$cur _pos> $buffer _size) {$buffer = Fread ($fp, $bUffer_size); Echo $buffer; $cur _pos + = $buffer _size; } $buffer = Fread ($fp, $file _size-$cur _pos); Echo $buffer; Fclose ($FP); return true; }
Make sure it is the same file but the downloaded file appears smaller than the source file.
Reply content:
1. Functions based on downloading files on the Web
Public Function Putfile ($file _dir, $file _name) {$file _dir = Chop ($file _dir);//Remove extra spaces in the path//Get the path of the file to download if ($file _dir! = ") {$file _path = $file _dir; if (substr ($file _dir,strlen ($file _dir) -1,strlen ($file _dir))! = '/') $file _path. = '/'; $file _path. = $file _name; } else $file _path = $file _name; Determine if the file to be downloaded exists if (!file_exists ($file _path)) {//alert (' Sorry, the file you want to download does not exist '); return false; } $file _size = FileSize ($file _path); Header ("CONTENT-TYPE:APPLICATION/OCTET-STREAM;CHARSET=GBK"); Header ("Accept-ranges:bytes"); Header ("Accept-length: $file _size"); Header ("content-disposition:attachment; Filename= ". $file _name); $fp = fopen ($file _path, "R"); $buffer _size = 1024; $cur _pos = 0; while (!feof ($fp) && $file _size-$cur _pos> $buffer _size) {$buffer = Fread ($fp, $bUffer_size); Echo $buffer; $cur _pos + = $buffer _size; } $buffer = Fread ($fp, $file _size-$cur _pos); Echo $buffer; Fclose ($FP); return true; }
Make sure it is the same file but the downloaded file appears smaller than the source file.
It's normal that different systems have differences.
Write too complex, the more complex the more error-prone, not sure if you have the length of management problems, the following code is sufficient.
$fp = fopen($file_path,"r");$content = '';while($_content = fread($fp, 1024)) $content.= $_content;fclose($fp);
If the file is only 100 bytes, fread ($FP, 1024) will not get 1024 bytes.
Other than that... I think that CHARSET=GBK should be removed without the need to specify the encoding.