简体   繁体   English

我的PHP反序列化有什么问题?

[英]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) (这是在twitter.com上列出的一组域,这些域作为键,而一个时间戳记作为值)

If I call: 如果我打电话给:

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

( "./neptune_output/recent" is the location of the $recentfile ) "./neptune_output/recent"$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. 我尝试将fopen模式设置为'c+''a+b'但它不起作用。

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." a +的意思是:“打开以进行读取和写入;将文件指针放在文件的末尾。如果文件不存在,请尝试创建它。”

If you just want to read "r" is enough: 如果您只想读“ r”就足够了:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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