Problems with a PHP shell script: "Could not open input file"
Ok, I am trying to 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.
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.
When executing the script via SSH I get the following error: Could not open input file: âq
So once again I thought that was the script telling me, that is could not read from STDIN
So I tried just a simple script.
#!/usr/local/bin/php –q
<?php
echo 'Hello World';
?>
Same thing: Could not open input file: âq
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 开发者_JAVA技巧CHMOD 755 I am really stumped on this.
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.
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.
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;
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.
I faced this same error while because I was using parameters in my php file path like this:
/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 " "
.
Windows Character Encoding Issue
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.
When you use php CLI argument -q doesn't exist.
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:
When \r was deleted from first line (mcedit shown \r as ^M) script ran correctly.
Due to windows encoding issue for me
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.
The error ws only occurring for me when the file transited through windows.
I know its stupid but in my case i was outside of my project folder i didn't have spark file.
For me the problem was I had to use /usr/bin/php-cgi
command instead of just /usr/bin/php
php-cgi is the command run when accessed thru web browser.
php is the CLI command line command.
Not sure why php cli is not working, but running with php-cgi instead fixed the problem for me.
精彩评论