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