3

I wrote a custom remove function in PHP. The function call is used recursively, but when calling the function, I get an error output:

// custom delete directory function
function deleteDirectory($dir) {
    if (!file_exists($dir)) return true;
    if (!is_dir($dir) || is_link($dir)) return unlink($dir);
    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') continue;

        // THIS IS LINE 32 LIKE MENTIONED IN THE ERROR
        if (!$this->deleteDirectory($dir . "/" . $item)) {

             // THIS IS LINE 33 LIKE MENTIONED IN THE ERROR
            chmod($dir . "/" . $item, 0777);
            if (!$this->deleteDirectory($dir . "/" . $item)) return false;
        };
    }

    // THIS IS LINE 37 LIKE MENTIONED IN THE ERROR
    return rmdir($dir);
}

function cleanup() {
    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->workFolder), RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($files as $name => $fileObject) {
        if (is_file($name)) {
            unlink($name);
        } else if (is_dir($name)) {
            system("/bin/chmod -R 0777 $name");
            $this->deleteDirectory($name);
        }
    }
    $this->deleteDirectory($this->workFolder);
}

I tried the approach with "$this->deleteDirectory()" but now I receive a totally different error:

Warning: rmdir(/tmp/533aab0362830/.) [function.rmdir]: Invalid argument in /usr/www/users/kontug/api.medifaktor.de/webservice/passkit/class/Pass.php on line 37

Warning: rmdir(/tmp/533aab0362830/../.ICE-unix) [function.rmdir]: Operation not permitted in /usr/www/users/kontug/api.medifaktor.de/webservice/passkit/class/Pass.php on line 37

Warning: chmod() [function.chmod]: Operation not permitted in /usr/www/users/kontug/api.medifaktor.de/webservice/passkit/class/Pass.php on line 33

Warning: rmdir(/tmp/533aab0362830/../.ICE-unix) [function.rmdir]: Operation not permitted in /usr/www/users/kontug/api.medifaktor.de/webservice/passkit/class/Pass.php on line 37
konturgestaltung
  • 467
  • 6
  • 19
  • 2
    Where you call the function ? – Debflav Apr 01 '14 at 11:58
  • And what's on line 32 of `/webservice/passkit/class/Pass.php`? – ghoti Apr 01 '14 at 11:59
  • 3
    Function is part of a class: missing `$this->` or static reference. – Salieri Apr 01 '14 at 12:00
  • You should debug step by step (`var_udmp($dir)`) – Debflav Apr 01 '14 at 12:16
  • I tried the var_dump, but without any outcome. I thought about the "operation not permitted", but I set the chmod to 0777 before. – konturgestaltung Apr 01 '14 at 12:19
  • possible duplicate of [A recursive remove directory function for PHP?](http://stackoverflow.com/questions/1407338/a-recursive-remove-directory-function-for-php) – d.raev Apr 01 '14 at 12:21
  • I think you have an issue with PHP language. What the `$dir` print at the beginning of the method ? Throw an Exception if `$dir` is empty. Maybe you don't have the right permission on the folder so you're not able to use the `chmod()`. – Debflav Apr 01 '14 at 12:29

2 Answers2

2

If the function belongs to a class then it should be called like this

$this->deleteDirectory($dir . "/" . $item)

instead of

deleteDirectory($dir . "/" . $item)
dikesh
  • 2,977
  • 2
  • 16
  • 26
  • 1
    If it were part of a class, wouldn't it be a "method"? – ghoti Apr 01 '14 at 12:06
  • Yes you are right @ghoti http://stackoverflow.com/questions/4841605/what-is-a-difference-between-a-method-and-a-function – dikesh Apr 01 '14 at 12:09
  • 2
    yes, we would call it a method, but php does not say 'method' in error's, or in class definition. So the term 'function' is a bit amibuous here. So in fact @Dikesh is (most probably) right in his suggestion. – giorgio Apr 01 '14 at 12:10
1

When you use classes you must call the method as $this->methodName().
Below is snipped which worked for me.
You can try this. If it gives permission error then you can add chmod function.
Remember you can not traverse a directory if you don't have read permission. So set the permission first.

   function deleteDirectory($dir) {
        if (!file_exists($dir)) return true;
        if (!is_dir($dir)) return unlink($dir);
        foreach (scandir($dir) as $item) {
            if ($item == '.' || $item == '..') continue;
            if (!$this->deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false;
        }
        return rmdir($dir);
    }
Vivek Muthal
  • 995
  • 1
  • 10
  • 23
  • I tried this method you suggested: and inserted the following line after "foreach" and before the first if statement: chmod($dir . "/" . $item, 0777); But this is not accepted by the script – konturgestaltung Apr 01 '14 at 12:25
  • I mean if you are trying to access a directory outside the /var/www (i.e root of execution) folder. Then from terminal set the permission. Not from the code. In code don't set permission in recursive function. – Vivek Muthal Apr 01 '14 at 12:30
  • ah okay. I am creating dynamically temp folders with the sys_get_temp_dir() function. How can I set the permission to them? – konturgestaltung Apr 01 '14 at 12:32
  • Rather than I'll say use something like this before calling the deleteDirectory method. system("/bin/chmod -R $mod $dir"); – Vivek Muthal Apr 01 '14 at 12:32
  • This seems to work, but I still get an error for the line: return rmdir($dir); (Invalid Argument, and operation not permitted); Warning: rmdir(/tmp/533ab2ea4a033/.) [function.rmdir]: Invalid argument in /usr/www/users/kontug/api.medifaktor.de/webservice/passkit/class/Pass.php on line 35 Warning: rmdir(/tmp/533ab2ea4a033/../.ICE-unix) [function.rmdir]: Operation not permitted in /usr/www/users/kontug/api.medifaktor.de/webservice/passkit/class/Pass.php on line 35 – konturgestaltung Apr 01 '14 at 12:35
  • Operation not permitted means you are running the command as www-data user. You have to be root or any other user if you want access to other directories you can try this "sudo /bin/chomod..." if you are on ubuntu this will run it as root. – Vivek Muthal Apr 01 '14 at 12:38
  • This error is just because user www-data don't have permission to delete those files and directories. Use that system function with sudo set permission and then call delete method. – Vivek Muthal Apr 01 '14 at 12:40
  • just one more question! :) The "sudo /bin/chmod" can be called within PHP? – konturgestaltung Apr 01 '14 at 12:44
  • yes it can be called in php it will make a System call but I'll say first try without sudo. More info about exec, system is here http://stackoverflow.com/questions/732832/php-exec-vs-system-vs-passthru – Vivek Muthal Apr 01 '14 at 12:47
  • I just tried the call without sudo, but still no difference..Sorry. I don't want to bug you! I just edited the question: The cleanup() method. Is this correct? – konturgestaltung Apr 01 '14 at 12:48
  • Use sudo then because current user doesn't have enough permission to modify it. – Vivek Muthal Apr 01 '14 at 12:49
  • I did this as well: system("sudo /bin/chmod -R 0777 $files"); But still no change.. I am really sorry I am not up to speed here :) – konturgestaltung Apr 01 '14 at 12:51
  • Do I call the system function at the right place at all? – konturgestaltung Apr 01 '14 at 12:52
  • not $files it should be parent of directory eg. /temp. So it will recursively set the permission. – Vivek Muthal Apr 01 '14 at 12:52
  • put the system function immediately after opening cleanup function. – Vivek Muthal Apr 01 '14 at 12:54
  • it will look like this function cleanup(){ system("/bin/chmod -R 0777 $this->workFolder"); ... – Vivek Muthal Apr 01 '14 at 12:56
  • So I tried this: function cleanup() {system("/bin/chmod -R 0777 $this->workFolder"); (...)} But still the same message. – konturgestaltung Apr 01 '14 at 12:56
  • ohh.. then try this system("/usr/bin/sudo /bin/chmod -R 0777 $this->workingFolder"); – Vivek Muthal Apr 01 '14 at 13:01
  • You won't believe it Vivek... still no change.... I still get the Operation not permitted – konturgestaltung Apr 01 '14 at 13:02
  • hmm... from terminal are you able to set the permission? change the $this->working folder to a constant like /temp then try – Vivek Muthal Apr 01 '14 at 13:03
  • 1
    I believe after setting the permission from terminal your issue will be solved. Hope so. Best Luck. – Vivek Muthal Apr 01 '14 at 13:07
  • I don't seem to have access by terminal to the server... I think I don't have root access – konturgestaltung Apr 01 '14 at 13:09