Home > Article > Backend Development > What to do if php downloads doc garbled characters
The solution to garbled php downloaded doc: first open the corresponding code file; then clean it through "ob_end_clean()" before the Header.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
PHP download DOC garbled code?
I recently built a system that required downloading the doc file
After the previous code was downloaded, it was always garbled when opened...
I haven’t found it on Google for a long time. Solution
Finally got it done later
Must be cleaned before Header, that is, ob_end_clean()
$file_size = filesize($logName); ob_end_clean(); header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Accept-Length:$file_size"); header("Content-Disposition:attachment;filename=" . $fileName); $fp = fopen($logName, "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;
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of What to do if php downloads doc garbled characters. For more information, please follow other related articles on the PHP Chinese website!