把你要保存的數(shù)據(jù)序列化一下,保存到文本中,如果需要修改,則從文本中取出,在反序列化化,在修改后,在序列化一下,保存到文本中。
成都創(chuàng)新互聯(lián)公司專注于天津網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供天津營(yíng)銷型網(wǎng)站建設(shè),天津網(wǎng)站制作、天津網(wǎng)頁(yè)設(shè)計(jì)、天津網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造天津網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供天津網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
文本的讀寫 參考fopen 等函數(shù)
使用form表單post數(shù)據(jù)到PHP,然后用file_put_contents($fileName, $data)寫入文件,$fileName是文件名,$data是要寫入的數(shù)據(jù)
新建一個(gè)a.php文件,將下面的復(fù)制進(jìn)去訪問(wèn)一下,填寫后點(diǎn)擊提交,會(huì)生成一個(gè)a.txt的文件,里面是你填寫的內(nèi)容
可能會(huì)有一個(gè)notice的報(bào)錯(cuò),不必理會(huì)
?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
$name = addslashes(stripslashes($_POST['name']));//主要是反轉(zhuǎn)義用戶輸入的個(gè)別轉(zhuǎn)義字符,然后統(tǒng)一轉(zhuǎn)義;
$password = $_POST['pwd'];
$content = $name."\t".$password."\r\n";
$f = fopen('test.txt', 'a+');//我猜測(cè)你應(yīng)該是想累加存儲(chǔ),在文件的結(jié)尾插入,所以用了fopen和a+,
fwrite($f, $content);
fclose($f);
在提取post中的之前,嚴(yán)謹(jǐn)點(diǎ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());
//項(xiàng)目路徑目錄,判斷是否存在,不存在則創(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ū)懼付ㄎ募?,如果文件不存則創(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)閉指針
? ? }
假如你已經(jīng)定義了get請(qǐng)求方法
$r = get('網(wǎng)址');
file_put_contents('保存的文件名',$r);
文章標(biāo)題:php存儲(chǔ)數(shù)據(jù)到文件中,php保存文件到本地
地址分享:http://m.kartarina.com/article48/hddehp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站排名、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站改版、手機(jī)網(wǎng)站建設(shè)
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)