execute php from command line

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
bladecatcher
Forum Commoner
Posts: 67
Joined: Sat Mar 12, 2005 12:50 am

execute php from command line

Post by bladecatcher »

G'day All,
I'm using php5 on Debian woody.

I've installed php5-cgi.

How do I execute a php file (myfile.php) from the command line?

I've tried #php myfile.php, #php5 myfile.php and a few others but always get command not found.

Thanking you in anticipation,
bladecatcher
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Can you run /usr/bin/php5?

If so, then you can: For example:

Code: Select all

#!/usr/bin/php5 -f
<?php
$func = 'trim';
echo '<' . $func('  test  ') . '>';
?>
Otherwise you can:
  • Code: Select all

    $ whereis php5
  • Code: Select all

    $ apt-get install php5-cli
  • then follow those 3 first steps listed above
Hope it helps!

-- Scorphus

Hail to Debian
bladecatcher
Forum Commoner
Posts: 67
Joined: Sat Mar 12, 2005 12:50 am

now to add to cron job

Post by bladecatcher »

G'day scorphus,
Thank you for your reply. my prob was php5-cli was not installed.

But, another problem arises.
If I execute it from root I get a prob when the script tries to access a file in the same directory.
/# php5 /var/www/test/myfile.php

Warning: I/O warning :failed to load external entity "forecast.xml"

I can execute it from the directory without problems.
server:/var/www/test# php5 myfile.php

What is the command to run the above in a cron job?

Thanking you (again) in anticipation,
bladecatcher

btw, great reply! I learnt a bit more than I asked. :-)
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Re: now to add to cron job

Post by scorphus »

Hi! You're welcome, I'm glad to help. :)
bladecatcher wrote:(..) I get a prob when the script tries to access a file in the same directory (..)
Well, now it is a programming issue. Your script might be looking for that "forecast.xml" file in the current directory. When running your script from a directory that's not the one which holds the "forecast.xml" you have to find a way to tell it what is the path to the XML file. Fortunately, PHP has an easy way to do it: there is a magic constant called __FILE__ that holds the full path and filename of the script file. For example let's take a simple script that uses this constant:

Code: Select all

#!/usr/bin/php5 -f
<?php
$fileDir = dirname(__FILE__) . '/'; //їurl=http://www.php.net/dirname]dirnameї/url], їurl=http://www.php.net/language.constants.predefined]__FILE__ї/url]
readfile($fileDir . 'dirname.php'); //їurl=http://www.php.net/readfile]readfileї/url]
?>
Please consider reading those references: dirname, __FILE__ and readfile

Let's run it:

Code: Select all

ї~]
їscorphus]@їKilauea] $ pwd
/home/scorphus
ї~]
їscorphus]@їKilauea] $ l /home/scorphus/lab/php/dirname.php
4 -rwxr-xr-x  1 scorphus scorphus 221 2005-04-01 04:07 /home/scorphus/lab/php/dirname.php*
ї~]
їscorphus]@їKilauea] $ /home/scorphus/lab/php/dirname.php
#!/usr/bin/php5 -f
<?php
$fileDir = dirname(__FILE__) . '/'; //їurl=http://www.php.net/dirname]dirnameї/url], їurl=http://www.php.net/language.constants.predefined]__FILE__ї/url]
readfile($fileDir . 'dirname.php'); //їurl=http://www.php.net/readfile]readfileї/url]
?>
ї~]
їscorphus]@їKilauea] $
Considering the fact that your PHP script file and the XML file are both on the same directory, you have a way to tell your script where "forecast.xml" resides.
bladecatcher wrote:(..) What is the command to run the above in a cron job? (..)
You might want to setup a cron job in /etc/crontab. Open it with your favourite text editor (i.e. $ vi /etc/crontab), there you'll find instruction to add a new job.
bladecatcher wrote:(..) btw, great reply! I learnt a bit more than I asked. :-)
Nice! That's exactly what I wanted to happen!

Regards,
Scorphus.
bladecatcher
Forum Commoner
Posts: 67
Joined: Sat Mar 12, 2005 12:50 am

Great stuff

Post by bladecatcher »

G'day scorphus,
Hope they pay you heaps ;-) damn good.

Code: Select all

// set name of XML file
$path = (dirname(__FILE__) . '/');
$file = $path . 'forecast.xml'
//$file = "/var/www/test/weather/forecast.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
I've got it working simply using the absolute path "/var/www/test/forecast.xml", however I want to work out if I can use dirname(__FILE__), I want to work it out on my own if possible, best way to learn, I'll get back to you when I'm totally stuck.

Having fun, learning heaps,
Thanks again,
bladecatcher
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Re: Great stuff

Post by scorphus »

bladecatcher wrote:G'day scorphus,
Hope they pay you heaps ;-) damn good. (...)
Heh, I hope too... :)
bladecatcher wrote:(...) I've got it working simply using the absolute path "/var/www/test/forecast.xml", however I want to work out if I can use dirname(__FILE__), I want to work it out on my own if possible, best way to learn, I'll get back to you when I'm totally stuck.

Having fun, learning heaps,
Thanks again,
bladecatcher
Very good, this is the right path, keep on it and you'll learn whatever you want to. Fell free to get back when stuck or, best yet, to help others (what is more interesting). Don't forget to keep the PHP Manual at hand. Also keep going out, having some beer and lot of fun!

Cheers,
Scorphus.
Post Reply