AshleyW
AshleyW

Reputation: 23

Issue with deleting directory and files

Im trying to, once a button is clicked, delete a directory with any files and additional directories within that however I am encountering a problem.

The error I receive is -

PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(http://[email protected]/): failed to open dir: not implemented' in

And here is the section of code this relates to -

$dir = 'http://www.thisismylink.co.uk/userfolder/' . $row['email'] . "/";
    $it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
    $files = new RecursiveIteratorIterator($it,
                RecursiveIteratorIterator::CHILD_FIRST);
    foreach($files as $file) {
        if ($file->isDir()){
            rmdir($file->getRealPath());
        } else {
            unlink($file->getRealPath());
        }
    }
    rmdir($dir);

Now currently the structure is -

User Email ($dir)

What I want to do is delete everything inside the 'User Email' directory as well as the 'User Directory' itself aswell

Upvotes: 0

Views: 182

Answers (2)

Patrick Webster
Patrick Webster

Reputation: 290

This is really a rather dangerous way to handle the issue; I'm not trying to circumvent your actual question, however please consider keeping a database full of "directories" and who they are associated to. When you remove a user, set a flag in the db to "toBeRemoved" or something of the like.

Then every so often, have a cron / scheduled task come through and query a list of directories to remove, then delete the database entry / mark it updated.

The problem is that no matter how secure you believe it to be, a script can be exploited, or produce unpredictable results from web/user based input.

In the event these errors are permission based, using the above mentality would allow this to be done at the "root" user level without fear of system files being deleted at the hand of your code being mangled by a bad user (or well intended user hitting the wrong combination of events).

Upvotes: 0

MegaAppBear
MegaAppBear

Reputation: 1220

Seems like you're using the full URL as you top directory. Shouldn't it be:

$dir = 'userfolder/' . $row['email'] . "/"; //Relative to your script
$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it,
            RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
    if ($file->isDir()){
        rmdir($file->getRealPath());
    } else {
        unlink($file->getRealPath());
    }
}
rmdir($dir);

Upvotes: 1

Related Questions