Monday, July 7, 2008

Linux: Call Linux Commands Inside PHP Page

At times, we need to verify some linux binaries from our web hosting provider. Say, we need to confirm path locations of specific linux binaries in order for our particular PHP pages to work out its function. There are lot of ways to do it, the simplest way is to call your web hosting provider for technical support, ask them your question or send them an email or even chat with them - simply to ask where a particular command resides on from their server. If you are in doubt about their hopeful replies to you, or its late Sunday and nobody is inside their office, what do you do now?

Simply create a simple PHP page that calls a few linux command inside your PHP page and echo back to you the data that you need to know.

Say, we need to know the exact path location of linux binary mysql and mysqldump, here's a simple way to do the job.

Below is a sample PHP page to call a few linux commands and echo its current path location.

<?php
echo exec('/usr/bin/which mysqldump');
echo '<br>';
echo exec('whereis mysql');
echo exec('touch 1.txt');
?>


Upload the file to your webserver and point your browser to it to echo your needed results.

The above command does the following job by displaying it on a web page

1. Echo the exact path location of mysqldump binary

2. Echo the exact path location of mysql binary

3. And lastly, create a zero-sized file named 1.txt under current working web folder.

That's it.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy