PHP shell 脚本的问题:“无法打开输入文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4567688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems with a PHP shell script: "Could not open input file"
提问by Mike L.
Ok, I am tryingto create an email logger, that uses a PHP shell script. I have set up CPanel to pipe emails to my script. I am sure this is all configured properly. However I am having problems with the script, well any script for that matter when running it from the shell.
好的,我正在尝试创建一个使用 PHP shell 脚本的电子邮件记录器。我已经设置了 CPanel 来通过管道将电子邮件发送到我的脚本。我确信这一切都配置正确。但是,我遇到了脚本问题,以及从 shell 运行它时与此相关的任何脚本。
here is an example.
这是一个例子。
#!/usr/local/bin/php –q
<?php
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("mail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
/* Script End */
?>
Real simple, right? Read from STDIN and write to a file...I thought something was wrong, not able to read STDIN for some reason. Hosting provider allows it, allow_url_open and allow_url_include are both on.
真的很简单吧?从 STDIN 读取并写入文件...我以为出了什么问题,由于某种原因无法读取 STDIN。主机提供商允许它,allow_url_open 和 allow_url_include 都打开。
When executing the script via SSH I get the following error: Could not open input file: aq
通过 SSH 执行脚本时,出现以下错误:无法打开输入文件:aq
So once again I thought that was the script telling me, that is could not read from STDIN
所以我再一次认为这是脚本告诉我的,那就是无法从 STDIN 读取
So I tried just a simple script.
所以我只尝试了一个简单的脚本。
#!/usr/local/bin/php –q
<?php
echo 'Hello World';
?>
Same thing: Could not open input file: aq
同样的事情:无法打开输入文件:aq
So it appears that the PHP program is telling me it is unable to open the script? The script is located in $HOME/mail/forward (CHMOD 755) and the script itself is CHMOD 755, as well the file mail.txt is CHMOD 755 I am really stumped on this.
所以看起来PHP程序告诉我它无法打开脚本?该脚本位于 $HOME/mail/forward (CHMOD 755) 中,脚本本身是 CHMOD 755,而文件 mail.txt 也是 CHMOD 755 我真的被这个难住了。
采纳答案by Daniel Trebbien
Have you tried:
你有没有尝试过:
#!/usr/local/bin/php
I.e. without the -q
part? That's what the error message "Could not open input file: -q" means. The first argument to php
if it doesn't look like an option is the name of the PHP file to execute, and -q
is CGI only.
即没有-q
部分?这就是错误消息“无法打开输入文件:-q”的含义。php
如果它看起来不像选项的第一个参数是要执行的 PHP 文件的名称,并且-q
仅是 CGI。
EDIT:A couple of (non-related) tips:
编辑:几个(不相关的)提示:
- You don't need to terminate the last block of PHP with
?>
. In fact, it is often better not to. - When executed on the command line, PHP defines the global constant
STDIN
tofopen("php://stdin", "r")
. You can use that instead of opening"php://stdin"
a second time:$fd = STDIN;
- 你不需要用
?>
. 事实上,通常最好不要这样做。 - 在命令行上执行时,PHP 将全局常量定义
STDIN
为fopen("php://stdin", "r")
. 您可以使用它而不是"php://stdin"
第二次打开:$fd = STDIN;
回答by JustinP
I just experienced this issue and it was because I was trying to run a script from the wrong directory.. doh! It happens to the best of us.
我刚刚遇到了这个问题,这是因为我试图从错误的目录中运行脚本..doh!它发生在我们中最好的人身上。
回答by programer
When you use php CLI argument -q doesn't exist.
当您使用 php CLI 参数时 -q 不存在。
I had the same problem when I wrote script in the Windows (eclipse) and I tried run them on Linux. Every line in file from Windows is ended by \r\n. I had to delete \r in first line that contained parser path:
当我在 Windows (eclipse) 中编写脚本并尝试在 Linux 上运行它们时,我遇到了同样的问题。来自 Windows 的文件中的每一行都以 \r\n 结束。我不得不删除包含解析器路径的第一行中的 \r :
When \r was deleted from first line (mcedit shown \r as ^M) script ran correctly.
当从第一行删除 \r 时(mcedit 显示 \r 为 ^M)脚本正确运行。
回答by Joseph Lust
Windows Character Encoding Issue
Windows 字符编码问题
I was having the same issue. I was editing files in PDT Eclipse on Windows and WinSCPing them over. I just copied and pasted the contents into a nano window, saved, and now they worked. Definitely some Windows character encoding issue, and not a matter of Shebangs or interpreter flags.
我遇到了同样的问题。我在 Windows 上的 PDT Eclipse 中编辑文件并 WinSCPing 它们。我只是将内容复制并粘贴到纳米窗口中,保存,现在它们可以工作了。肯定是一些 Windows 字符编码问题,而不是 Shebangs 或解释器标志的问题。
回答by Ram Shengale
I landed up on this page when searching for a solution for “Could not open input file”error. Here's my 2 cents for this error.
在搜索“无法打开输入文件”错误的解决方案时,我登陆了此页面。这是我对这个错误的 2 美分。
I faced this same error while because I was using parameters in my php file path like this:
我遇到了同样的错误,因为我在我的 php 文件路径中使用了这样的参数:
/usr/bin/php -q /home/**/public_html/cron/job.php?id=1234
But I found out that this is not the proper way to do it. The proper way of sending parameters is like this:
但我发现这不是正确的方法。发送参数的正确方式是这样的:
/usr/bin/php -q /home/**/public_html/cron/job.php id=1234
Just replace the "?"
with a space " "
.
只需用"?"
空格替换" "
。
回答by jsherk
For me the problem was I had to use /usr/bin/php-cgi
command instead of just /usr/bin/php
对我来说,问题是我必须使用/usr/bin/php-cgi
命令而不仅仅是/usr/bin/php
php-cgi is the command run when accessed thru web browser.
php-cgi 是通过 Web 浏览器访问时运行的命令。
php is the CLI command line command.
php 是 CLI 命令行命令。
Not sure why php cli is not working, but running with php-cgi instead fixed the problem for me.
不知道为什么 php cli 不起作用,但是使用 php-cgi 运行反而为我解决了这个问题。
回答by steampowered
Due to windows encoding issue for me
由于我的 Windows 编码问题
I experienced this "Could not open input file" error. Then I obtained the file using wget
from another linux system, and the error did not occur.
我遇到了“无法打开输入文件”错误。然后我wget
从另一个linux系统中获取了该文件,并没有出现错误。
The error ws only occurring for me when the file transited through windows.
该错误仅在文件通过 Windows 传输时发生。