Home  >  Article  >  Backend Development  >  What to do if php downloads doc garbled characters

What to do if php downloads doc garbled characters

藏色散人
藏色散人Original
2021-05-26 10:11:431665browse

The solution to garbled php downloaded doc: first open the corresponding code file; then clean it through "ob_end_clean()" before the Header.

What to do if php downloads doc garbled characters

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!

Statement:
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