0

我想打开文件,检查文件中是否不存在字符串 deos 写入它。

这样做:

$fp=fopen('categories.txt','a+');
$content=fread($fp,filesize('categories.txt'));

if(!strstr($content,$cat)){
    fwrite($fp,','.$cat);
}
fclose($fp);

但是我在写完之后在 categories.txt 中得到了重复的值。我能预料到的唯一问题是编码问题,但所有文件都是 utf-8 并且categories.txt我只有拉丁符号和几个符号。

任何想法问题出在哪里?

4

2 回答 2

2

试试这样。

$pos = strpos($content, $cat);
if($pos === false){
fwrite($fp,','.$cat);
}
于 2012-02-16T09:50:11.547 回答
0

好的,我认为问题在fopen. 我改成这个,代码开始工作:

$content=file_get_contents('categories.dat');
$type=(string) $type;
$content=(string)$content;

if(!strstr($content,$type)){
    file_put_contents('categories.dat',$content.','.$type);
}

感谢您的帮助。

于 2012-02-16T11:53:13.097 回答