20

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 CHMOD 755 I am really stumped on this.

2
  • 1
    What text editor do you use for php code? The "-" in your "-q" looks like an en dash instead of a minus. Commented Dec 31, 2010 at 3:55
  • I get the same problem on Mac OS 14.1. The file exists. I'm in the same folder as the file but php cannot open input file. I tried a very simple file, just phpinfo.php and the obvious contents. Opened the permissions on the file wide and it just doesn't work. Commented Jan 7, 2024 at 13:16

9 Answers 9

33

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.

Sign up to request clarification or add additional context in comments.

Comments

8

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 " ".

Comments

6

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:

  1. You don't need to terminate the last block of PHP with ?>. In fact, it is often better not to.
  2. When executed on the command line, PHP defines the global constant STDIN to fopen("php://stdin", "r"). You can use that instead of opening "php://stdin" a second time: $fd = STDIN;

4 Comments

Thanks, when I removed the -q, I received another error which was fopen("mail.txt") wanted the absolute path /home/user/mail/forward/mail.txt I did not think about that since I was outside of my public_html directory php needed an absolute path. My question is, how can I suppress any output if it is not -q? If in the event of an error, the script outputs anything, the user who sent the email will get a bounce-back so everything I have read on email piping all want you to use -q
To suppress warnings for any function call, you can use an at sign: $fp = @fopen("mail.txt").
So there is not a CLI switch to suppress output of a script?
@Mike: If you really want to disable script output caused by warnings, errors, etc., then at the top of your script, you simply call the error_reporting function with the value 0: us2.php.net/manual/en/function.error-reporting.php
4

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.

1 Comment

I had a similar problem on Windows 10. I ended up doing a str_replace on all \ to /. In other words: str_replace('\\', '/', $filePath)
4

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.

Comments

2

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.

Comments

1

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.

Comments

1

I know its stupid but in my case i was outside of my project folder i didn't have spark file.

1 Comment

This is essentially the same as the top-voted answer from 2013.
0

The actual root cause is that the hyphen before "q" on the first line is actually not a standard ASCII hyphen(U+002D), but an en-dash(U+2013). The "a" with the funny hat that PHP reported was the first clue, and then if you look closely the "dash" is too big. This is the kind of thing word processors do to code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.