PHP搜索和替换不使用file_put_contents [关闭]

This must be a really simple issue but I've been trying for an hour to solve. I have the following code:

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

And the test.txt contents are:

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

For some reason google never gets replaced by yahoo. My question is, how do I replace google with yahoo?

Thanks

If you issue the php command from a different directory then your code can't find the file.

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

Changing directory to the location of the php and txt files makes it work.

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