无法使用 PHP CLI 读取文件
$file=file('DATA.txt');
我不断收到“无法打开流。...中没有这样的文件或目录”。当我在服务器和浏览器上运行它时,这是有效的。但是当我使用命令行运行脚本时出现错误。
$file=file('DATA.txt');
I keep getting "failed to open stream. No such file or directory in ...". This works when I run it on a server and browser. But the error occurs when I run the script using command line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先深吸一口气,喝杯茶;)
消息是:
DATA.txt
只是一个文件名。文件通常放置在目录中。尽管这听起来很愚蠢,但实际上也很愚蠢:目录是什么?
答案:当前工作目录。这在服务器和 CLI 之间有所不同。
因此最好也添加您的目录:
希望这有帮助。
First of all take a deep breath and a tea ;)
The news is:
DATA.txt
is only a file-name.A file normally is placed in a directory. As stupid as this sounds, well, as stupid this is: What is the directory?
The answer: the current working directory. This differs between servers and CLI.
So better add your directory as well:
Hope this is helpful.
这个问题肯定缺乏一些澄清,但有两个猜测:
web
,例如),并期望此文件位于此用户主目录(或准确地说是当前目录)中。The question definitely lacks some clarifications, but two guesses:
web
, for example), and expects this file to be located in this user home dir (or current dir, to be precise).我发现这有效:
$fname=str_replace(basename(__FILE__),'',__FILE__).'DATA3.txt';
我不能简单地使用“dirname”,因为我希望它在 Windows 和 Linux 中都能工作(\ 或 /)。
I found that this works:
$fname=str_replace(basename(__FILE__),'',__FILE__).'DATA3.txt';
I couldn't simply use "dirname" because I wanted this to work in both Windows and Linux (\ or /).