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
execute php from command line
Moderator: General Moderators
-
bladecatcher
- Forum Commoner
- Posts: 67
- Joined: Sat Mar 12, 2005 12:50 am
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
Can you run /usr/bin/php5?
If so, then you can:
Otherwise you can:
-- Scorphus
Hail to Debian
If so, then you can:
Code: Select all
#!/usr/bin/php5 -fCode: Select all
$ chmod +x script-file.phpCode: Select all
$ ./script-file.php
Code: Select all
#!/usr/bin/php5 -f
<?php
$func = 'trim';
echo '<' . $func(' test ') . '>';
?>Code: Select all
$ whereis php5Code: Select all
$ apt-get install php5-cli- then follow those 3 first steps listed above
-- Scorphus
Hail to Debian
-
bladecatcher
- Forum Commoner
- Posts: 67
- Joined: Sat Mar 12, 2005 12:50 am
now to add to cron job
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.
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.
- 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
Hi! You're welcome, I'm glad to help. 
Please consider reading those references: dirname, __FILE__ and readfile
Let's run it:
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.
Regards,
Scorphus.
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:bladecatcher wrote:(..) I get a prob when the script tries to access a file in the same directory (..)
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]
?>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] $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:(..) What is the command to run the above in a cron job? (..)
Nice! That's exactly what I wanted to happen!bladecatcher wrote:(..) btw, great reply! I learnt a bit more than I asked.
Regards,
Scorphus.
-
bladecatcher
- Forum Commoner
- Posts: 67
- Joined: Sat Mar 12, 2005 12:50 am
Great stuff
G'day scorphus,
Hope they pay you heaps
damn good.
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
Hope they pay you heaps
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!");Having fun, learning heaps,
Thanks again,
bladecatcher
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
Re: Great stuff
Heh, I hope too...bladecatcher wrote:G'day scorphus,
Hope they pay you heapsdamn good. (...)
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!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
Cheers,
Scorphus.