PHP Not Executing on Command Line - php

Hello I have php working fine with my apache server. However, when executing the php script from the command line (like /usr/bin/php file.php) it just echos the sourcecode of my script back at me, even when executing a PHP in the web directory. Anybody know how to fix this?

Command line scripts STILL need to have at least <?php to switch over to PHP mode. Remember that there's really no such thing as a PHP script. There's only files which have a <?php ... ?> code blocks within them.
So, something like
#!/usr/bin/php
echo 'hello world!';
Would actually output
echo 'hello world!';
because There's no actual PHP code in there. There's just some text that LOOKS like php code. You need to have
#!/usr/bin/php
<?php
echo 'hello world!';
and then you'll get the expected
hello world!
as output. Without <?php the php interpreter will never "switch" over to actual PHP mode, and just treat all of the text in the file as plain output.

In your past questions, you seem to prefer using short open tags (<?), so that is probably the issue. Try running this instead:
/usr/bin/php -d short_open_tag=yes file.php
If this works, then your current config has short_open_tag disabled. You'll have to edit your CLI config file to change this. You can find the location of the config file using this:
/usr/bin/php -i | grep php.ini
You should see:
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini
Edit the file listed by "Loaded Configuration File". The line with:
short_open_tag = Off
Should be set to On.

I have been fighting this issue for over 4 months and finding nothing to fix it.
My problems started when I installed a new 3T drive and then moved all my projects off the default 1.5T drive. Could never get anything working right in "localhost" since. Also cannot get aliases working in the 2 virtual hosts I created, but their index.html files now show fine at:
projects.com
tbnk-svr.com
But none of the following work:
localhost/phpmyadmin,
localhost/seopanel,
localhost/test.php,
localhost/info.php,
php on the command line.
I wrote this up at:
https://www.linuxquestions.org/questions/newreply.php?do=newreply&noquote=1&p=6214546
and since I get no errors in any of the error logs, can not figure out what is denying localhost and the cmd line from working. Test of:
php <<< "<?php echo 1;"
php -v
Both work, so not sure why it is only echoing the file contents as "cat file" would do.
Has to do somehow with it not rendering "localhost"
Cheers!
TBNK

Related

Issues with exec() on php running a shell script

I'm trying to use a php script for executing a shell script. However I'm having some issues apparently with the web server.
I have a bash script called switch_audio.sh. It basically changes the active audio output of the system.
I also have a script.php that runs the code below:
<?php
echo "It's working";
exec("/var/www/html/switch_audio.sh");
?>
When I execute php script.php it's working fine. However, when I try to run it on the web browser by localhost/script.php I just get as ouput the "echo" part.
I've already tried to:
remove 'exec()' from the disable functions in php.ini;
give permissions for everybody in every folder on this path from "/" to the localhost folder;
Any thoughts about it?
Simple solution: exec() returns a string, but you also have to output it to the user:
<?php
echo "It's working";
echo exec("/var/www/html/switch_audio.sh");
?>
You already said you allowed the shell function on your php.ini configuration but it seems it's not working yet... so maybe:
You didn't restarted webserver service after changes
You have somewhere other statement more restrictive... maybe ini_set in your php files.
As a reminder, be sure that you are doing well on your php.ini file, check it for this kind of statement:
disable_functions=exec,passthru,shell_exec
Maybe you can try instead of exec other similar php function like shell_exec to check if it works. Or maybe is working and not showing anything.

Run php in command line on CentOS7

I expected this to be quite simple but I cannot make it work.
I'm trying to run a php script in command line (to finally be able to automate it in a cron.)
The file is outside of the public web folder to avoid executing it from a webbrowser.
File is called 'daily.php' and contains :
#!/usr/bin/php
<?
echo "hello \n";
file_put_contents("daily.log", "executed...", FILE_APPEND | LOCK_EX);
?>
then in my terminal I run (as root):
/usr/bin/php daily.php
but it simple outputs me the full source code without the hashbang.
So I tried changing the file to 755, tried to chmod +x it but still outputs me the full source code.
I had a look in the man page and I found :
-f <file> Parse and execute <file>.
Tried this but still the same output.
Why is this? how can I interpret this file?
Ok
I found the answer after 2 hours of trouble, and just after posting this question.
Even if you hashbang the file, even if you call php directly, you need to implicitly tell it is php by using :
<?php
?>
and not
<?
?>

How to execute external PHP script in command line from your browser

I have an external php script named external.php which includes this:
<?php
echo 'External Output';
?>
When I run that on command line with command:
php external.php
I get output as
External Output
But when the same script I execute from my browser's php file named index.php which has this code:
<?php
$exe=exec('php external.php',$out,$ret);
print_r($out);
?>
Then I get no output.
When I modify it as:
<?php
$exe=exec('php external.php 2>&1',$out,$ret);
print_r($out);
?>
Then I get this output:
php: /opt/lampp/lib/libxml2.so.2: version `LIBXML2_2.9.0' not found (required by php)
My question is, how can I execute that "external.php" file in commandline from index.php and get output on my browser (i.e. on index.php) ? I have tried system() function too, it doesn't work as well.
Edit:
I cannot include the external.php in index.php because external.php can take lot of time to execute (more than 10 hours).
Edit#2
Solved it, giving the full path to PHP solved the problem.
Here is an example:
<?php
$exe=exec('full/path/to/php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Thankyou Oleg and Scopey for giving me some hint help.
Specify the full path of external.php:
<?php
$exe=exec('php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Try also specifying the full path to php on the command line and check that it works:
$/usr/bin/php "/full/path/to/external.php"
If that works, try it also in your php file:
<?php
$exe=exec('/usr/bin/php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Try running phpinfo() from the command line. Often installations of PHP use a different PHP.ini for CLI usage. Running phpinfo() will show you the location of the ini file in use. It appears as though this ini might not be configured properly for your installation (as a guess)

Run bash script to create file in PHP

In addition to my previous question, another problem appeared and I decided to make a new question for it:
I am currently calling a php script that than runs a bash script. The php script looks like:
chdir('/home/');
$output = shell_exec('./do.sh');
echo "<pre>$output</pre>";
The do.sh contains:
#! /bin/bash
echo 12;
dd if=/dev/urandom of=test.test bs=1048576 count=2
The problem is following:
When I call ./do.sh from the terminal everything works fine: test.test is created and the ouput is 12
However, when I call it from my php file, the output is 12 aswell, but no file is being created. Since I know almost nothing about bash scripting, I have no idea why this is happening...
Check if PHP safe_mode is enabled. You have to turn it off in your /etc/php.ini file, and obviously check filesystem permissions.

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 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 to fopen("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.
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.

Categories