简体   繁体   中英

What's wrong with my PHP unserialize?

I have a file with the contents:

a:12:{s:12:"a2.twimg.com";i:1308768611;s:12:"a1.twimg.com";i:1308768611;s:12:"a0.twimg.com";i:1308768612;s:12:"a3.twimg.com";i:1308768612;s:8:"this.com";i:1308768613;s:15:"f.prototype.com";i:1308768613;s:15:"c.prototype.com";i:1308768614;s:15:"a.prototype.com";i:1308768614;s:5:"w.com";i:1308768615;s:5:"s.com";i:1308768615;s:5:"f.com";i:1308768615;s:5:"h.com";i:1308768615;}

(It's an array of domains listed on twitter.com as keys and a timestamp as values)

If I call:

unserialize(fread($recentfile, filesize("./neptune_output/recent"))) 

( "./neptune_output/recent" is the location of the $recentfile )

It fails, but if I call unserialize with that string pasted in, it works.

I use the following code to open the file.

$recentfile = fopen("./neptune_output/recent", 'a+')

I've tried putting the fopen mode as 'c+' and 'a+b' but it won't work.

Do I need to post more code?

您为什么不只用file_get_contents()来阅读它,而不是把它打开并弄清楚文件大小呢?

a+ means: "Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it."

If you just want to read "r" is enough:

$recentfile = fopen("./neptune_output/recent", 'r')

See http://nl2.php.net/manual/en/function.fopen.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM