Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with a PHP shell script: "Could not open input file"

Tags:

bash

shell

php

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.

like image 582
Mike L. Avatar asked Dec 31 '10 02:12

Mike L.


People also ask

Could not open input file error in php?

1 Answer. You run php files files by invoking the interpreter which is php . This also shows you the direct path to all your php version which you can also use to run your php scripts. Also make sure you have read rights on that file as the user you run php with.

What is PHP shell script?

PHP Shell or Shell PHP is a program or script written in PHP (Php Hypertext Preprocessor) which provides Linux Terminal (Shell is a much broader concept) in Browser. PHP Shell lets you to execute most of the shell commands in browser, but not all due to its limitations.


4 Answers

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.

like image 117
JustinP Avatar answered Oct 13 '22 23:10

JustinP


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;
like image 38
Daniel Trebbien Avatar answered Oct 13 '22 23:10

Daniel Trebbien


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.

like image 29
Joseph Lust Avatar answered Oct 13 '22 22:10

Joseph Lust


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.

like image 23
programer Avatar answered Oct 13 '22 22:10

programer