Use serialize PHP array to a File

Some time ago in the development of micro-channel public platform, we plan to use session temporarily store data query, but it is not a perfect public platform browser can not store COOKIE, can not save the session_id, that is, not take session. Ever since the use serialize () function to an array written to the file, when we need to use this array will read the file out and then use unserialize () to an array.

Writes an array of file functions

1
2
3
4
5
6
7
8
9
10

* An array written to a file
* @param unknown_type $ fileName
* @param unknown_type ARR $
* /
function (fileName $, $ ARR) {
$ FP = the fopen ($ fileName, 'WB' );
fwrite ($ FP, the serialize ($ ARR));
fclose ($ FP);
}

Conversion to read the file into an array

Big Box   PHP using serialize the array to save the file lass = "highlight php">
1
2
3
4
5
6
7
8
9
10
11
12
13
14

将文件中的内容读出并转为数组
@param unknown_type $fileName
@return mixed
*/
function file2array(fileName) {
if(!file_exists(fileName) {
exit($fileName." don't exists");
}
fp=fopen(fp=fopen(fileName, ‘rb’);
str=fread(str=fread(fp, filesize(fileName));
returnunserialize(fileName));
returnunserialize(str);
}

Note: The article may be many mistakes, and there may be cases unusable occurs because the technology blog is my study notes, I just record some of the see or think of something, so I do not recommend you to just follow the content of the blog post use. Thank you ~ ~

Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11711227.html
Recommended