How to run a system command from a PHP page.
Using PHP to run system command on the server side can come in very useful in many occasions.
Some examples:
- Using an online storage service for your site that doesn’t give you shell access.
- Running this during an application logic.
I will give here the main idea of this technique and a few code examples of useful commands to use with it.
Downloading a file to the server:
When I upgrade my WordPress installation I don’t want to download all the files to my personal PC and then re-upload them to the server so what I do is, I use this command to download the files:
php system(‘wget http://url-to-download-location/filename.zip’); ?>
Then I unzip the file using this command:
php system(‘unzip -o filename.zip’); ?>
The -o means it will overwrite existing files without asking so I would use that one with caution. be warned.
As you probably gathered by now the way to run system commands from php is very simple, just put the code:
php system(‘command’); ?>
And that is it. all you have to do now is go to your browser and access the URL of the php file.
http://yoursite/yourPHPfile.php
And the command will run =]
’till next time, have a good one =]
Nadav…
Tags: linux, php, system command, web