纯文本文件的奇怪file_get_contents()行为&没有扩展


Bizarre file_get_contents() behavior with plain-text files & no extension?

在过去的几天里,我一直在本地应用程序上工作,我注意到我的一个'exec()'函数调用外部程序没有正确启动。经过进一步调查,很明显程序确实执行了,但是它过早地退出了,因为使用'file_get_contents()'的重要行没有检索指定文件的内容。

文件为明文文件,不带扩展名。我猜'file_get_contents()'正在将文件视为目录,因为没有扩展名?这很奇怪,因为如果我在web浏览器中手动执行相同的程序,一切都运行得很好。

下面是一个清晰的示例行-

while(file_get_contents('plaintextfile') == "something"){
/// Do This
    }

当我从web浏览器访问/program.php时,上面的工作很好,但是当像这样调用它时,它会给我一个文件/文件夹未找到'plaintextfile'的错误。

exec('php /program.php', $output);
foreach($output as $output){
print $output . "<br>";
}

提前感谢任何可以阐明这种情况的人。

从浏览器和命令行(在exec()调用中)执行的PHP可能使用不同的PHP .ini配置,并且可能具有不同的文件搜索路径。最好的做法是提供plaintextfile的完整路径。

if(!file_get_contents('/path/to/plaintextfile')){
   // file couldn't be read
}