Run php in command line on CentOS7 - php

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
<?
?>

Related

Running PHP Script In Powershell keeps opening NotePad ++ rather than executing the PHP script

Okay, I'm at a bit of a loss here.
I'm testing out running PHP scripts from within powershell and it just keeps opening NotePad ++ rather than executing the script. I cannot figure out why this won't work...
I'm using a pretty basic PHP script to test:
<?php
echo 'Hello, World!';
?>
And I'm calling it using the standard way I run .ps1 files:
PS C:\php> c:\phpfiles\test25.php
The execution policy is set to unrestricted... what am I doing wrong?
You should pass the path of the file as an argument to the PHP executable. If (lets say) PHP is installed in c:\php, then you must do:
PS c:\php\php.exe -f c:\phpfiles\test25.php

PHP Not Executing on Command Line

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

Redirection inside system command is not working?

I have a php code in which I am trying to redirect the output of a command to a file My code is
<?php
shell_exec("ls >txt.txt");
?>
But when I checked the file, nothing was inside!.This was working when I tried this command line, what might be the issue?
Redirection is a shell (e.g. bash) feature so it won't work here. You need to do something like:
shell_exec("bash -c 'ls > txt.txt'");

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.

Most correct way to run php at the linux shell

Is there a correct way to use php from the command line...or rather...is one way more correct than another ?
If you create a file, say test.php with the following code:
#!/usr/bin/php
<?php
print "This is a test".PHP_EOL;
print "This is another test!!";
?>
then chmod +x text.php (make it executable on linux).
You can then run in the following ways.....
./test.php
or
php test.php
I prefer just using ./test.php, but often see php test.php in examples.
ALSO
is the following correct syntax for the shebang line
#!/usr/bin/php
or is this more correct
#!/usr/bin/php -q
I've seen both, and see that the -q flag is to quiet the html stuff, but was wondering if
php compiled with cli compatibility really needs the -q flag ???
Thanks for your help :)
#!/usr/bin/php
On the first line of an interpreter script, the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains
"#! /bin/sh"
, then the contents of the file are executed as a shell script.
In your case it means excute the script using the php file in /usr/bin location.
Using php test.php means that you are running your test.php with php so u don't need the first line. where as for ./test.php your file needs to be executable and first line is required.
And about the -q flag look at this
specifically on
rob 23-Mar-2007 11:48
There are better expanations if you see for -q. here's another one
If you are writing little php scripts for your own purposes, #! is fine.
If they are to be on some kind of world visible box (e.g. web server), then I'd say not so fine - since you have made them executable they are now a security risk.
I tend to use #! for perl, sh (which are always little private, non production things) and php somefile.php for PHP (which may or may not end up on a server).

Categories