Problems with a PHP shell script: “Could not open

2019-04-04 11: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.

标签: php bash shell
7条回答
够拽才男人
2楼-- · 2019-04-04 12:20

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.

查看更多
登录 后发表回答