1

I created a button that links to finish.php. It is supposed to delete the installer directory. The problem is that it fails on the first try, but will work on subsequent reloads of the page:

function Delete($path){ 
    if (is_dir($path) === true){
        $files = array_diff(scandir($path), array('.', '..'));

        foreach ($files as $file){
            Delete(realpath($path) . '/' . $file);
        }
        return rmdir($path);
    }else if (is_file($path) === true){
        return unlink($path);
    }
    return false;
}
Delete('installer');

$filename = '../admin/installer/';
if (file_exists($filename)) { Delete('installer'); } else {header("Location: index.php");}
6
  • Hmmmmm....anything in the error logs? Commented Mar 11, 2015 at 18:29
  • Can you just make a shell call? It's a whole lot simpler. exec("rm -rf {$path}");. Commented Mar 11, 2015 at 18:31
  • I think there is some caching issue... Commented Mar 11, 2015 at 18:33
  • 1
    you're looking if ../admin/installer exists, but then start trying to delete what boils down to ./installer. those would be two completely different directories, unless your current directory happens to be admin. Commented Mar 11, 2015 at 18:34
  • 1
    If you do an exec you have to be sure that the $path does not come from user. Because you can execute another command. Commented Mar 11, 2015 at 18:35

1 Answer 1

1

I think your delete function is not stable.

You can use this function to delete folder, delete all it's files and folders :

public static function deleteDir($dirPath) {
    if (! is_dir($dirPath)) {
        throw new InvalidArgumentException("$dirPath must be a directory");
    }
    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
        $dirPath .= '/';
    }
    $files = glob($dirPath . '*', GLOB_MARK);
    foreach ($files as $file) {
        if (is_dir($file)) {
            self::deleteDir($file);
        } else {
            unlink($file);
        }
    }
    rmdir($dirPath);
}

Source : https://stackoverflow.com/a/3349792/3444315

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.