PHP shell_exec is not working. How to fix?

68 Views Asked by At

Im trying to make a page on linux server and i will use some shell command in php

But there are some problems with my code, Shell command is not working

My web php is here

<?php

$list = shell_exec("ls");
echo $list;

$remove = shell_exec("rm -rf testFolder");
echo $remove;

?>

Thats all, list command is working on php but remove command is not working.

Also it's working from terminal well even safe_mode is off

Do you know about this problem? (PHP version is 5.4 and i checked all config)

1

There are 1 best solutions below

3
Bhavin Solanki On

You have to change the file permission before removing files. Also set the full file or directory path.

<?php
$result = '';
$remove = realpath("/myproject/testFolder");
chmod($remove, 0777); //make sure I can handle the file
if(file_exists($remove)){ //Make sure that the file exists
    $result = shell_exec("rm -rf $remove");
}
var_dump($result);
?>