Solution to the garbled output of php binary stream: 1. Open the local "conn.php" and "print.php" files; 2. Use "ob_clean" to clear the header content, and modify the code such as "mysql_close(); ob_clean();header("Content-type:$type");".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
php What should I do if the binary stream output is garbled?
I recently used PHP to develop and read and output binary files from mysql, and encountered a problem with garbled characters.
Generally, the following method is used to output binary files:
<?php if(!isset($id) or $id=="") die("error: id none"); //定位记录,读出 $conn=mysql_connect("127.0.0.1","***","***"); if(!$conn) die("error : mysql connect failed"); mysql_select_db("test",$conn); $sql = "select * from receive where id=$id"; $result = mysql_query($sql); $num=mysql_num_rows($result); if($num<1) die("error: no this recorder"); $data = mysql_result($result,0,"file_data"); $type = mysql_result($result,0,"file_type"); $name = mysql_result($result,0,"file_name"); mysql_close($conn); //先输出相应的文件头,并且恢复原来的文件名 header("Content-type:$type"); header("Content-Disposition: attachment; filename=$name"); echo $data; ?>
There is no problem with the above method. But if you encapsulate the database connection in a separate file, there will be problems. Rewrite the above code into 2 files:
//conn.php <?php function Open_DB(){ $conn=mysql_connect("127.0.0.1","***","***"); if(!$conn) die("error : mysql connect failed"); mysql_select_db("test",$conn); } ?>
//print.php <?php if(!isset($id) or $id=="") die("error: id none"); //定位记录,读出 require_once('conn.php'); Open_DB(); $sql = "select * from receive where id=$id"; $result = mysql_query($sql); $num=mysql_num_rows($result); if($num<1) die("error: no this recorder"); $data = mysql_result($result,0,"file_data"); $type = mysql_result($result,0,"file_type"); $name = mysql_result($result,0,"file_name"); mysql_close(); header("Content-type:$type"); header("Content-Disposition: attachment; filename=$name"); echo $data; ?>
At this time, garbled characters will be generated when calling print.php to open the word file. The problem lies in the "require_once('conn.php')" statement. When PHP calls this statement, it will be output in the header, which affects the following two header statements, thereby destroying the data flow of the word file. Therefore, the opened word file will be garbled.
The solution is to use ob_clean to clear the header content. The rewritten print.php is as follows
//print.php <?php if(!isset($id) or $id=="") die("error: id none"); //定位记录,读出 require_once('conn.php'); Open_DB(); $sql = "select * from receive where id=$id"; $result = mysql_query($sql); $num=mysql_num_rows($result); if($num<1) die("error: no this recorder"); $data = mysql_result($result,0,"file_data"); $type = mysql_result($result,0,"file_type"); $name = mysql_result($result,0,"file_name"); mysql_close(); ob_clean(); header("Content-type:$type"); header("Content-Disposition: attachment; filename=$name"); echo $data; ?>
Recommended study: "PHP Video Tutorial"
The above is the detailed content of What to do if php binary stream outputs garbled characters. For more information, please follow other related articles on the PHP Chinese website!