在php中為我們提供了一個函數var_export 他可以直接將PHP代碼入到一個文件中,代碼如下:
- var_export($times,true);//後面不加true不能寫入文件喲
- $fp = fopen('aa.txt','w+');
- fwrite($fp,var_export($times,true));
- fclose($fp);
方法二,利用serializ與unserialize函數,代碼如下:
- if(isset($_POST['sub'])){
- $cfg = array('contact'=>$_POST['contact']); //把數據存入數組
- file_put_contents('./data/contact.cache',serialize($cfg));
- //把數組序列化之後,寫到contact.cache裡,
- $this->redirect('other/contact');//跳轉
- }//開源代碼PHPfensi.com
- else{
- $fp = fopen('./data/contact.cache','r');//讀
- $cf = unserialize(fread($fp,filesize('./data/contact.cache')));//反序列化,並賦值
- $this->assign('cfg',$cf);//送到前台模板
- $this->display('other/contact');
- }