20

I need to execute shell commands from controller , but not only for files inside the project , ex. system('rm /var/www/html/test.html') or system('sudo unzip /var/www/html/test.zip');

I call the function but nothing happen , any idea how to execute external shell commands from controller like removing one file in another directory?

system('rm /var/www/html/test.html');
//or
exec('rm /var/www/html/test.html')
0

3 Answers 3

41

If you're wanting to run commands from your PHP application I would recommend using the Symfony Process Component:

  1. Run composer require symfony/process

  2. Import the class in to your file with use Symfony\Component\Process\Process;

  3. Execute your command:

    $process = new Process(['rm', '/var/www/html/test.html']);
    
    $process->run();
    

If you're using Laravel, you should be able to skip Step 1.


Alternatively, (if the process running php has the correct permissions) you could simply use PHP's unlink() function to delete the file:

unlink('/var/www/html/test.html');
 
9
  • i tried this but Symfony Process executes commands only inside project directory , when I do $process = new Process(['rm', '/var/www/another_directory/test.html']) , it doesnt execute
    – AnonS
    Jan 19, 2019 at 13:40
  • @AnonS In that case I would imagine you have a permissions issue. What operating system are you using?
    – Rwd
    Jan 19, 2019 at 16:00
  • i am using linux
    – AnonS
    Jan 19, 2019 at 21:04
  • you were right , it was permission issue ,i gave it 755 and www-data and it works like charm , thanks
    – AnonS
    Jan 20, 2019 at 15:19
  • 1
    You don't need to install it, it's already included with Laravel. See stackoverflow.com/a/27857423/135101. Mar 11, 2021 at 19:09
3

I would do this with what the framework already provide:

1) First generate a command class:

php artisan make:command TestClean

This will generate a command class in App\Console\Commands

Then inside the handle method of that command class write:

@unlink('/var/www/html/test.html');

Give your command a name and description and run:

php artisan list

Just to confirm your command is listed.

2) In your controller import Artisan facade.

use Artisan;

3) In your controller then write the following:

Artisan::call('test:clean');

Please refer to the docs for further uses: https://laravel.com/docs/5.7/artisan#generating-commands

4
  • this will not work for my situation ,anyway thanks, good solution
    – AnonS
    Jan 19, 2019 at 13:44
  • 1
    @AnonS I think like Ross mentioned its a permission issue, maybe you should simply check what output you get from exec('rm /var/www/html/test.html', $output); var_dump($output); and share it with us
    – Eden Reich
    Jan 19, 2019 at 17:28
  • the output is empty
    – AnonS
    Jan 19, 2019 at 21:04
  • 1
    my bad that should of be exec('rm /var/www/html/test.html 2>&1', $output); var_dump($output);
    – Eden Reich
    Jan 20, 2019 at 2:00
1

Use fromShellCommandline to use direct shell command:

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

$process = Process::fromShellCommandline('rm /var/www/html/test.html');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

echo $process->getOutput();
1
  • 1
    i have forgot $process= Process::fromShellCommandline('..... Jul 27 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.