這篇文章主要介紹了php二進(jìn)制流輸出亂碼如何解決的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇php二進(jìn)制流輸出亂碼如何解決文章都會(huì)有所收獲,下面我們一起來看看吧。
創(chuàng)新互聯(lián)公司專注于愛輝企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),商城網(wǎng)站開發(fā)。愛輝網(wǎng)站建設(shè)公司,為愛輝等地區(qū)提供建站服務(wù)。全流程按需定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
php二進(jìn)制流輸出亂碼的解決辦法:1、打開本地“conn.php”和“print.php”文件;2、用“ob_clean”清空header內(nèi)容,修改代碼如“MySQL_close();ob_clean();header("Content-type:$type");”。
php 二進(jìn)制流輸出亂碼怎么辦?
最近在用php開發(fā)從mysql中讀取并輸出二進(jìn)制文件,遇到了亂碼問題。
一般輸出二進(jìn)制文件是用下面的方法:
用上面的方法是沒有問題的。但如果把database 連接封裝在一個(gè)單獨(dú)的文件中,就有問題了。改寫上面的代碼為2個(gè)文件: 這時(shí)候調(diào)用print.php打開word文件時(shí)會(huì)產(chǎn)生亂碼。問題就出在"require_once('conn.php')"語句。php在調(diào)用該語句時(shí)會(huì)在header中輸出,這影響到了后面的2個(gè)header語句,從而破壞了word文件的數(shù)據(jù)流。因此打開的word文件會(huì)是亂碼。<?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);
//先輸出相應(yīng)的文件頭,并且恢復(fù)原來的文件名
header("Content-type:$type");
header("Content-Disposition: attachment; filename=$name");
echo $data;
?>
//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;
?>
解決的方法是用ob_clean清空header內(nèi)容。改寫的print.php 如下
//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;
?>
關(guān)于“php二進(jìn)制流輸出亂碼如何解決”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“php二進(jìn)制流輸出亂碼如何解決”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:php二進(jìn)制流輸出亂碼如何解決
分享路徑:http://aaarwkj.com/article10/jjpsgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站排名、Google、靜態(tài)網(wǎng)站、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)