十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
使用form表單post數(shù)據(jù)到PHP,然后用file_put_contents($fileName, $data)寫入文件,$fileName是文件名,$data是要寫入的數(shù)據(jù)
通州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
新建一個(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
// 生成一個(gè)PHP數(shù)組
$data = array();
$data['name'] = 'admin';
$data['pwd'] = '123456';
// 把PHP數(shù)組轉(zhuǎn)成JSON字符串
$json_string = json_encode($data);
// 寫入文件
file_put_contents('user.json', $json_string);
// 從文件中讀取數(shù)據(jù)到PHP變量
$json_string = file_get_contents('user.json');
// 把JSON字符串轉(zhuǎn)成PHP數(shù)組
$data = json_decode($json_string, true);
// 顯示出來(lái)看看
var_dump($data)
php導(dǎo)出數(shù)據(jù)excel有專門的庫(kù),當(dāng)導(dǎo)出少量數(shù)據(jù)的時(shí)候速度很快,但是當(dāng)數(shù)據(jù)量大的時(shí)候就會(huì)存在服務(wù)器內(nèi)存不夠之類的。
所以在導(dǎo)出大量數(shù)據(jù)的時(shí)候就應(yīng)該分頁(yè)查詢數(shù)據(jù),避免服務(wù)器宕機(jī)。正好PHP提供了fputcsv函數(shù)可以將數(shù)據(jù)寫入到csv文件中。
這樣我們就可以使用PHP對(duì)數(shù)據(jù)進(jìn)行分頁(yè)查詢,再寫入到csv文件中。
//記錄返回值
? ? $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ū)懼付ㄎ募绻募淮鎰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)閉指針
? ? }