纯文本文件和文件的奇怪 file_get_contents() 行为没有扩展名?
过去几天我一直在开发本地应用程序,我注意到我的一个用于调用外部程序的“exec()”函数没有正确触发。经过进一步调查,很明显该程序确实执行了,但它过早退出,因为使用“file_get_contents()”的重要行没有检索指定文件的内容。
该文件是没有扩展名的纯文本文件。我猜测“file_get_contents()”将文件视为目录,因为没有扩展名?这很奇怪,因为如果我从网络浏览器手动执行相同的程序,一切都会完美运行。
为了清楚起见,这里有一个示例行 -
while(file_get_contents('plaintextfile') == "something"){
/// Do This
}
当我从网络浏览器访问 /program.php 时,上面的代码运行得很好,但是当像这样调用它时,它会给我一个“plaintextfile”的文件/文件夹未找到错误。
exec('php /program.php', $output);
foreach($output as $output){
print $output . "<br>";
}
预先感谢任何能够阐明这种情况的人。我真的很困惑这个...
I've been working on a local app over the last few days and I've noticed that one of my 'exec()' functions to call an external program didn't fire correctly. Upon further investigation it was obvious that the program did execute, but it quit prematurely as an important line utilizing 'file_get_contents()' didn't retrieve the contents of the file specified.
The file is a plaintext file without an extension. I'm guessing that 'file_get_contents()' is treating the file as a directory since there is no extension? It's strange because if I manually execute the same program from a web browser, everything works perfectly.
Here's an example line for clarity -
while(file_get_contents('plaintextfile') == "something"){
/// Do This
}
The above works just fine when I visit /program.php from a web browser, but when calling it like this it gives me a file/folder not found error for 'plaintextfile'.
exec('php /program.php', $output);
foreach($output as $output){
print $output . "<br>";
}
Thanks in advance to anyone who can shed some light on this situation. I'm really puzzled by this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从浏览器执行的 PHP 和通过命令行执行的 PHP(在 exec() 调用中)可能使用不同的 php.ini 配置,并且可能具有不同的文件搜索路径。最好的做法是提供纯文本文件的完整路径。
PHP as executed from the browser and executed by the command line (in the
exec()
call) may use different php.ini configurations, and may have different file search paths. The best course of action is to supply the full path toplaintextfile
.