How to run system commands using php?

| | 1 min read

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.