Write an array to a file in php and then read it

巴扎黑
Release: 2016-11-24 11:44:14
Original
1721 people have browsed it

I have seen many open source projects before. The configuration information of some projects is generally written in files.
Some are written to constants in configuration files, and some are written to arrays.

However, those that are relatively complex and arrays have not been studied in depth. I took a rough look at the constants.
Generally, when modifying, it is more complicated. First, you need to read out all the configuration files,
Then replace the content through regular matching, and then write it into the file.

The one I used below This method is relatively simple and easy, I will post the code

Php code

if(isset($_POST['sub'])){                 
    $cfg = array('contact'=>$_POST['contact']);  //把数据存入数组             
    file_put_contents('./data/contact.cache',serialize($cfg));  
        //把数组序列化之后,写到contact.cache里,  
    $this->redirect('other/contact');//跳转  
 }  
 else{                
    $fp = fopen('./data/contact.cache','r');//读  
    $cf = unserialize(fread($fp,filesize('./data/contact.cache')));//反序列化,并赋值  
    $this->assign('cfg',$cf);//送到前台模板  
    $this->display('other/contact');  
 }
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!