Problems with a PHP shell script: "Could not open input file"
The error message Could not open input file: /path/to/script is one of the most confusing outputs when debugging a CLI application, particularly when you are certain the file exists and has correct permissions. In the scenario described, a developer attempts to build an email logger using cPanel's mail forwarding capabilities. They expect the system to pipe incoming email data into their script via STDIN, only to be met with this specific fatal error even after verifying that allow_url_fopen and allow_url_include are enabled.
The confusion stems from a fundamental misunderstanding of how the PHP CLI interpreter resolves executable paths versus how it handles input streams. When you execute ./script.php or invoke the script via the shebang line (#!/usr/local/bin/php -q), the operating system's shell attempts to match the command name against entries in your environment's $PATH. If the path provided in the shebang does not exist exactly as written, or if the shell cannot locate the executable at that location, PHP terminates immediately before it ever starts parsing the file content.
In the provided source material, the script header reads:
#!/usr/local/bin/php âq
Notice the character between the dash and the 'q'. It is an en-dash (â), not a hyphen (-). While this looks identical in many text editors, the PHP CLI binary expects a standard ASCII hyphen. If the file was saved with a non-standard dash, or if the shebang path /usr/local/bin/php does not resolve to an actual executable on that specific server environment, the shell fails to launch the interpreter correctly. The error message Could not open input file: ... is misleading here; it often indicates that the process failed to start because the command name itself was invalid or could not be resolved, rather than a failure to read STDIN.
To resolve this, you must verify the exact location of the PHP binary on your server. You can do this by running which php or whereis php via SSH. If the output differs from /usr/local/bin/php, update the shebang in your script to match the actual path. Additionally, ensure that the file itself is executable. While the user mentioned CHMOD 755, you must also check that the parent directories allow execution. A common pitfall occurs when running scripts with ./script_name.php from a directory where the current user lacks execute permissions on the folder itself.
Furthermore, the logic of reading from STDIN is sound for this use case. The cPanel mail forwarding feature indeed pipes email content to the script's standard input stream. The code using fopen("php://stdin", "r") is the correct approach for capturing that piped data without needing to parse command-line arguments. However, ensure that the script is being invoked correctly by the server's cron or mail handler. If the mail forwarding configuration passes the email content directly as a stream, the script must be invoked in a way that preserves that stream, often requiring the invocation to happen via a wrapper or specific cPanel module settings rather than a direct manual run.
If the shebang is corrected and permissions are verified, and the error persists, consider whether the PHP version configured for the CLI matches the one running the mail handler. Sometimes shared hosting environments have multiple PHP versions installed; if the mail system calls php from /usr/bin/php56 but your shebang points to /usr/local/bin/php74, the wrong interpreter might be invoked or the path resolution fails silently before the script loads.
Finally, simplify the invocation method during debugging. Instead of relying on the shebang, try running the script explicitly with the full path to the binary:
/usr/local/bin/php -q /home/user/mail/forward/script.php < email_content
If this works, the issue was strictly a path resolution or shebang encoding problem. If it still fails, the environment's PHP installation might be missing critical extensions or has restrictive CLI configurations that differ from the web server setup.
