Sunday, January 13th, 2008
Execute Linux commands in PHP by using backticks
You can execute linux commands within a php script - all you have to do is put the command line in backticks (`).
Recently, I had to upload an archive to a server which did not allow ssh. For files that I need to transfer, I just package them up in a neat archive and transfer the archive file via scp. Not having ssh access however, I uploaded the archive file via ftp and created a script which extracts the file the file looks like the following:
Upon loading up “extractarchive.php”, I can see the output of the tar command through the browser. I did this by using the echo function on the backticked command line - this is not necessary for the backticked command to be executed by php.
Of course, the PHP file will need to have the necessary permissions to execute commands and as always, proceed with caution. I myself do not like doing things this way, I’ve used it in the past as a last resort. But if you ever need to do something like this, at least you know that this can be done with PHP.
on Tuesday, August 5th, 2008 at 3:59 am:
You can also execute commands using exec(), system() and popen().
on Friday, August 15th, 2008 at 2:53 am:
OK, and the sintaxis to pass variables to the linux shell? exec(’tar -xzvf $anyfile’); does not seem to work
on Friday, August 15th, 2008 at 4:05 pm:
hmmm that seems odd, it should work, but well some servers I’ve come accross have disabled such php features. Maybe that’s the problem.
for example, the following excerpt produced some output for me.
– code
<?php
$cmd = 'ls -la'; // the linux command
$files = array(); // init the output array
exec($cmd, $files); // execute the linux command and put the output in output array
// dump the output
echo '<pre>';
print_r($files);
echo '</pre>';
Anyway, check out the php manual site for more info on exec, perhaps they’ll have some more information for you. :)
http://au2.php.net/manual/en/function.exec.php
on Wednesday, October 22nd, 2008 at 6:05 am:
thank you man ilove you php
on Wednesday, December 31st, 2008 at 11:01 pm:
Do I need to change some configuration in php.ini ???
on Thursday, January 29th, 2009 at 5:25 am:
It won’t work for me :(
$comand = “ls -la >> /var/www/deise.txt”;
$output = array();
exec($comand, $output);
print_r($output);
The result in the screen is:
Array()
Any ideas?
on Wednesday, February 4th, 2009 at 8:29 pm:
Are you pushing the listing (ls) to a txt file? Perhaps you should try listing the directory:
$comand = ?ls -la /var/www/?;
$output = array();
exec($comand, $output);
print_r($output);
on Tuesday, March 3rd, 2009 at 5:58 pm:
I tried executing a shell script via php to open applications like firefox,gvim. It is executing in the command line but not in the browser . Can any one tell me what is the solution for this?
Thanks in advance
on Sunday, May 17th, 2009 at 8:38 am:
Hmm it seems this only work in local directory,it can run files from other directory.Well could be beacuse of whm security settings.