Save PHP variables to a text file

后端 未结 7 1576
梦如初夏
梦如初夏 2020-11-29 00:59

I was wondering how to save PHP variables to a txt file and then retrieve them again.

Example:

There is an input box, after submitted the stuff that was writ

7条回答
  •  感情败类
    2020-11-29 01:36

    Personally, I'd use file_put_contents and file_get_contents (these are wrappers for fopen, fputs, etc).

    Also, if you are going to write any structured data, such as arrays, I suggest you serialize and unserialize the files contents.

    $file = '/tmp/file';
    $content = serialize($my_variable);
    file_put_contents($file, $content);
    $content = unserialize(file_get_contents($file));
    

提交回复
热议问题