PHP搜索和替换不工作file_put_contents


PHP search and replace not working with file_put_contents

这一定是一个非常简单的问题,但我已经尝试了一个小时来解决。我有以下代码:

foreach (glob("*.txt") as $filename)
{
    $file = file_get_contents($filename);
    file_put_contents($filename, str_replace("google","yahoo",$file));
}

test.txt含量为:

Fusce condimentum aliquam velit nec suscipit. 
Cras dapibus libero id ante volutpat laoreet. 
Aliquam luctus erat eget orci egestas, id tincidunt leo aliquam. 
google

由于某种原因google从未被yahoo取代。我的问题是,我如何用yahoo代替google ?

谢谢

如果你从另一个目录发出php命令,那么你的代码将无法找到该文件。

me@alfa:/tmp# cat test/test.txt 
Fusce condimentum aliquam velit nec suscipit. 
Cras dapibus libero id ante volutpat laoreet. 
Aliquam luctus erat eget orci egestas, id tincidunt leo aliquam.
google
me@alfa:/tmp# cat test/test.php 
<?php
foreach (glob("*.txt") as $filename)
{
    $file = file_get_contents($filename);
    file_put_contents($filename, str_replace("google","yahoo",$file));
}
?>
me@alfa:/tmp# php test/test.php 
me@alfa:/tmp# cat test/test.txt 
Fusce condimentum aliquam velit nec suscipit. 
Cras dapibus libero id ante volutpat laoreet. 
Aliquam luctus erat eget orci egestas, id tincidunt leo aliquam.
google

将目录更改为php和txt文件的位置使其工作。

me@alfa:/tmp# cd test/
me@alfa:/tmp/test# ls
test.php  test.txt
me@alfa:/tmp/test# cat test.txt 
Fusce condimentum aliquam velit nec suscipit. 
Cras dapibus libero id ante volutpat laoreet. 
Aliquam luctus erat eget orci egestas, id tincidunt leo aliquam.
google
me@alfa:/tmp/test# php test.php 
me@alfa:/tmp/test# cat test.txt 
Fusce condimentum aliquam velit nec suscipit. 
Cras dapibus libero id ante volutpat laoreet. 
Aliquam luctus erat eget orci egestas, id tincidunt leo aliquam.
yahoo