-1

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)

user3783243
  • 5,368
  • 5
  • 22
  • 41
Jayden
  • 1
  • 1
  • 1
    What user is php running as? What owner and permissions does testFolder have? – Keiji Aug 17 '23 at 13:02
  • 3
    I hope you're paying someone for extended PHP support, because [PHP 5.4 last had an official security patch nearly eight years ago](https://www.php.net/eol.php). – IMSoP Aug 17 '23 at 13:03
  • When diagnosing things like this, `echo` can hide things, you might want to try `var_dump` or similar – Chris Haas Aug 17 '23 at 13:16
  • 2
    Why don't you just use the php functions `scandir` and `unlink` (c.f.: https://www.php.net/manual/de/ref.dir.php) – schmauch Aug 17 '23 at 13:27
  • You fix this error just lire any other: get the **error message** that explains the problem, and then act accordingly – Your Common Sense Aug 17 '23 at 13:41
  • Finally i solved this issue You can fix like bellow chcon -R -t httpd_sys_rw_content_t [folder] – Jayden Aug 18 '23 at 08:47

1 Answers1

-1

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);
?>
Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46