Execute Linux commands in PHP by using backticks

January 13th, 2008 by rvdavid Leave a reply »

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:

<?php
#extractarchive.php
echo `tar -xvfz somearchive.tar.gz`;
?>

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.

You may also be interested in the following posts

Advertisement

12 comments

  1. Binny V A says:

    You can also execute commands using exec(), system() and popen().

  2. Robert says:

    OK, and the sintaxis to pass variables to the linux shell? exec(‘tar -xzvf $anyfile’); does not seem to work

  3. rvdavid says:

    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

  4. ahmed says:

    thank you man ilove you php

  5. Hasan says:

    Do I need to change some configuration in php.ini ???

  6. deisefontoura says:

    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?

  7. john says:

    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);

  8. ruby says:

    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

  9. Luka says:

    Hmm it seems this only work in local directory,it can run files from other directory.Well could be beacuse of whm security settings.

  10. Subha says:

    Hi every one
    I have developed a website and from that site I need to change the Linux System Time abut unable to chage the time.

  11. real_phantom says:

    Nice tips. Thanks for sharing this great information.

Leave a Reply