How to run system commands using php?
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY jijimol.john
3 years ago
Commands
0
comments comment

We can execute system commands using SHELL. shell_exec is a PHP function that returns the output as a string or null if it is a wrong output. Note that, this function is disabled when the PHP is running in a safe mode.

I had a requirement to remove all files from one of my folder using PHP. Here I am sharing a small snippet for removing all files in a folder via shell_exec.


<?php
 $deletefiles = 'rm -rf {folder name}/*';
 shell_exec($deletefiles);
 ?>

You can execute any system commands using shell_exec like the above example. Need any help, contact us.


RELATED ARTICLE

more_horiz
close

on 20th March 2015 / by fazeela.ma
Git is a control system for developing softwares. There are a number of git commands (Git reference). While searching bringing a commit from one branch to another, I came across an interesting topic called cherry picking. Cherry pick a commit? Sounds interesting!!! I have researched a bit to let know whether it would fit my context or not. Git Cherry pick This command is those cases, where we wanted to bring the commits from a particular branch to another. General syntax for cherry picking a particular commit is, git cherry-pick commit_hash_idLet us consider we have 3 branches as A, B, and C along with a master. Suppose, we would like to bring the last commit from branch C to B. In this case, make sure that we are the branch were we would like to bring the commit to. In this scenario, make sure you are on branch B, as we are planning to bring the commit from C to B. Now, a question can arise for those who is not very good with git. Don't we mention the branch from were the git commit to be taken? Nice question!! Why we need to specify that, as the hash code for every commit is unique. So we don't want to mention the branch from where we take commit. Git is an interesting software to explore, every day we see new commands to get the new functionalities integrated to it. Git Commands Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 12th August 2015 / by jijimol.john
rsync command is used to synchronize files and folder from source to destination. It may be same or different server. If the synchronization is in between different servers we have to use ssh credentials for connecting the two. Example given below: rsync -ravpzHogt --delete -e ssh {source-path} {sshuser_id}@{ip/site-address}:{destination-path} Some of the options of rsync listed below. r - rsync to copy directories recurssively. a - archive - presave almost anything but does not presave the hardlinks. For that we use -H. v - verbose, using this option we can see which files is transferring at that time. p - permission, this option helps to update the permission of remote file is same as source file permission. z - compress, this option helps to compress any data in files and sends it to destination. H - Hardlink, recreate hard links on the remote system to be the same as the local system. If we doesnot use this option, the hardlinks treated as normal files. o -Using the option, to set the owner of the files to be same as source file. g - group, rsync to set the group of destination file same as the source file. This helps when the receiving program is not run by super user only groups have option to sent and recieving the data. t - time, rsync the modification time along with files and folder and is updated on remote server. --delete - This option helps to delete files from destination folder which is not present in source directory. -e - command, This option allows to choose alternative remote shell program for rsync the files from source to destination server. You can run this code as terminal command. Hope this article is useful. Please get in touch with us to know more. Remote Sync Commands Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 21st August 2015 / by jijimol.john
We can execute system commands using SHELL. shell_exec is a PHP function that returns the output as a string or null if it is a wrong output. Note that, this function is disabled when the PHP is running in a safe mode. I had a requirement to remove all files from one of my folder using PHP. Here I am sharing a small snippet for removing all files in a folder via shell_exec. &lt;?php $deletefiles = 'rm -rf {folder name}/*'; shell_exec($deletefiles); ?&gt; You can execute any system commands using shell_exec like the above example. Need any help, contact us. Commands Leave a reply Your email address will not be published. Required fields are marker *
Leave a reply
Your email address will not be published. Required fields are marker *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.