php将数据保存为txt文件,PHP将数据写入txt文件

//记录返回值

$write_data_a = [

'html_url'  =>  $getUrl,

'ip'    => $this->get_real_ip(),

'time'  =>  date("Y-m-d H:i:s",time()),

'res'   => $response

];

//转化为JSON

$write_data_a = json_encode($write_data_a) . '||' . "\n";

$date = date("Y-m-d", time());

//项目路径目录,判断是否存在,不存在则创建

$lujing = "./360_mobile_res_sd";

if(!is_dir($lujing)){

mkdir(iconv("UTF-8", "GBK", $lujing),0777,true);

}

//文件,判断是否存在,不存在则创建

$TxtFileName = "./360_mobile_res_sd/" . $date . "_2.txt";

//以读写方式打写指定文件,如果文件不存则创建

if(file_exists($TxtFileName))

{

//存在,追加写入内容

file_put_contents($TxtFileName, $write_data_a, FILE_APPEND);

}

else

{

//不存在,创建并写入

if( ($TxtRes=fopen ($TxtFileName,"w+")) === FALSE){

exit();

}

if(!fwrite ($TxtRes,$write_data_a)){ //将信息写入文件

fclose($TxtRes);

exit();

}

fclose ($TxtRes); //关闭指针

}