PHP中,使用var_export函數(shù)即可將數(shù)組格式寫入到文件;示例如下:

創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的中陽網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
?php
$file?=?"chinawinxp.txt";
$content=array(
"name"="百度知道",
"company"="百度在線",
"city"="北京",
"other"=array(
"edu"="百度教育",
"jingyan"="百度經(jīng)驗",
)
);
file_put_contents($file,var_export($content,true)."\r\n",FILE_APPEND);?
//寫入結(jié)果
/**
array?(
'name'?=?'百度知道',
'company'?=?'百度在線',
'city'?=?'北京',
'other'?=?
array?(
'edu'?=?'百度教育',
'jingyan'?=?'百度經(jīng)驗',
),
)
*/
?
使用form表單post數(shù)據(jù)到PHP,然后用file_put_contents($fileName, $data)寫入文件,$fileName是文件名,$data是要寫入的數(shù)據(jù)
新建一個a.php文件,將下面的復(fù)制進去訪問一下,填寫后點擊提交,會生成一個a.txt的文件,里面是你填寫的內(nèi)容
可能會有一個notice的報錯,不必理會
?php
$data = $_POST['text'];
$fileName = 'a.txt';
file_put_contents($fileName, $data);
?
!doctype html
html
head
meta charset="utf-8"
titletest/title
/head
body
form action="./a.php" method="post"
textarea name="text" id="" cols="30" rows="10"/textarea
input type="submit" value="提交"
/form
/body
/html
php 寫入txt:
PHP
function writelog($str)
{
$open=fopen("log.txt","a" );
fwrite($open,$str);
fclose($open);
}
'a' 寫入方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創(chuàng)建之。
'a+' 讀寫方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創(chuàng)建之。
php txt 換行
"\r\n"
不可用單引號.
//記錄返回值
? ? $write_data_a = [
? ? ? ? 'html_url'? =? $getUrl,
? ? ? ? 'ip'? ? = $this-get_real_ip(),
? ? ? ? 'time'? =? date("Y-m-d H:i:s",time()),
? ? ? ? 'res'?? = $response
? ? ];
//轉(zhuǎn)化為JSON
? ? $write_data_a = json_encode($write_data_a) . '||' . "\n";
? ? $date = date("Y-m-d", time());
//項目路徑目錄,判斷是否存在,不存在則創(chuàng)建
? ? $lujing = "./360_mobile_res_sd";
? ? if(!is_dir($lujing)){
? ? ? ? mkdir(iconv("UTF-8", "GBK", $lujing),0777,true);
? ? }
//文件,判斷是否存在,不存在則創(chuàng)建
? ? $TxtFileName = "./360_mobile_res_sd/" . $date . "_2.txt";
? ? //以讀寫方式打?qū)懼付ㄎ募绻募淮鎰t創(chuàng)建
? ? if(file_exists($TxtFileName))
? ? {
//存在,追加寫入內(nèi)容
? ? ? ? file_put_contents($TxtFileName, $write_data_a, FILE_APPEND);
? ? }
? ? else
? ? {
//不存在,創(chuàng)建并寫入
? ? ? ? if( ($TxtRes=fopen ($TxtFileName,"w+")) === FALSE){
? ? ? ? ? ? exit();
? ? ? ? }
? ? ? ? if(!fwrite ($TxtRes,$write_data_a)){ //將信息寫入文件
? ? ? ? ? ? fclose($TxtRes);
? ? ? ? ? ? exit();
? ? ? ? }
? ? ? ? fclose ($TxtRes); //關(guān)閉指針
? ? }
第一種:
?php
$filename = 'test.txt';
$somecontent = "this is test string.";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "不能打開文件 $filename";
exit;
}
// 將$somecontent寫入到我們打開的文件中。
if (fwrite($handle, $somecontent) === FALSE) {
echo "不能寫入到文件 quote;{$filename}quote;";
exit;
}
echo "已把quote;{$somecontent}quote;寫入到文件quote;{$filename}quote;";
fclose($handle); //將指針關(guān)閉
} else {
echo "文件{$filename}不可寫.";
}
?
第二種:
?php
$filename = "test.txt";
$content = "this is test string.";
$put = file_put_contens($filename,$content);
if(!put)
exit("write failed");
echo "write success";
?
當(dāng)前題目:php把數(shù)據(jù)寫入文本文件 php向文本文件寫入內(nèi)容,應(yīng)該采用哪個文件操作函數(shù)
URL地址:http://chinadenli.net/article16/hghgdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、ChatGPT、定制網(wǎng)站、品牌網(wǎng)站制作、動態(tài)網(wǎng)站、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)