PHP exec rm -Rf not working for subdirectories

10,391

Solution 1

There is no need in asterisk in this command. If you want to remove directory together with files, remove the slash as well. Leaving the slash will delete files, but preserve directory.

Also check comments on this page: http://php.net/manual/en/function.unlink.php

Solution 2

It's probably because the user executing the script doesn't have privileges for deleting the directory(ies) or the shell isn't in the right directory.

If you're running this via apache, chances are the process is owned by www-data. If the "some_dir" directory doesn't allow for write access by www-data, then it won't work.

Since you're using a relative path (very dangerous), you need to ensure that the script is executing in the correct directory. I'd suggest you change the path to an absolute one if you can - "rm -rf /path/to/some_dir".

Share:
10,391
Mitja Felicijan
Author by

Mitja Felicijan

Embedded platforms developer.

Updated on June 27, 2022

Comments

  • Mitja Felicijan
    Mitja Felicijan almost 2 years

    I am trying to delete all contents in specific folder but it doesn't seem to effect subfolder but it should, because bash command does it from console.

    system('rm -Rf some_dir/*');