What to do if php binary stream outputs garbled characters

藏色散人
Release: 2023-03-17 07:50:01
Original
1979 people have browsed it

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");".

What to do if php binary stream outputs garbled characters

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;
?>
Copy after login

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);
}
?>
Copy after login
//print.php
<?php
if(!isset($id) or $id=="") die("error: id none");
//定位记录,读出
require_once(&#39;conn.php&#39;);
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;
?>
Copy after login

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(&#39;conn.php&#39;);
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;
?>
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!