0

I have a script (got it from somewhere here in StackOverflow, credits don't go to me!) to delete a folder + its contents. However, it's not working for me. After deleting the folder a record from my DB should be erased and this happens just fine. However, the folder doesn't get deleted and neither its contents! This is my code:

<?php

$filepath = dirname(__FILE__);
$gemeented = preg_replace( '#^(.*)/(.*?)/(.*?)/(.*?)/(.*?)/(.*?)$#', "$2", $filepath );
$plaatsd = preg_replace( '#^(.*)/(.*?)/(.*?)/(.*?)/(.*?)/(.*?)$#', "$4", $filepath );
$hrubriekd = preg_replace( '#^(.*)/(.*?)/(.*?)/(.*?)/(.*?)/(.*?)$#', "$5", $filepath );
$bedrijfn = preg_replace( '#^(.*)/(.*?)/(.*?)/(.*?)/(.*?)/(.*?)$#', "$6", $filepath );

$filepath2 = "http://".$gemeented.".url.nl/".$plaatsd."/".$hrubriekd."/".$bedrijfn."/";
$filepath3 = "http://".$gemeented.".url.nl/".$plaatsd."/".$bedrijfn."/";

echo $filepath2;


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

        foreach ($files as $file)
        {
            Delete(realpath($filepath2) . '/' . $file);
        }

        return rmdir($filepath2);
    }

    else if (is_file($filepath2) === true)
    {
        return unlink($filepath2);
    }

    return false;
}


?>

To make sure my $filepath2 is correct I echoed it, the result is:

http://dongen.mydomain.nl/s-gravenmoer/aandrijvingenenbesturingen/bedrijfsnaam/

That's exactly the folder I want gone, however, it ain't happening! Folder has CHMOD 755.

EDIT:

Just using $filepath won't work either, echo-ing that gives me:

/vhosts/mydomain.nl/subdomains/dongen/httpdocs/s-gravenmoer/aandrijvingenenbesturingen/bedrijfsnaam

2 Answers 2

1

I wasn't able to get the above script working, but I managed to find another script which works for me! Just enter the relative path and that's it!

$dirname = "../".$bedrijfn."/";
delete_directory($dirname);

function delete_directory($dirname) {
if (is_dir($dirname))
      $dir_handle = opendir($dirname);
   if (!$dir_handle)
      return false;
   while($file = readdir($dir_handle)) {
      if ($file != "." && $file != "..") {
         if (!is_dir($dirname."/".$file))
            unlink($dirname."/".$file);
         else
            delete_directory($dirname.'/'.$file);    
      }
   }
   closedir($dir_handle);
   rmdir($dirname);
   return true;
}

Hope it helps somebody out!

Sander

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

Comments

0

You can't use url to delete files, you should give unlink a filesystem path. (Edit: the same goes for rmdir)

3 Comments

That's what I thought! But just using $filepath wouldn't work, that why I created $filepath2. $filepath echo gives me /vhosts/mydomain.nl/subdomains/dongen/httpdocs/s-gravenmoer/aandrijvingenenbesturingen/bedrijfsnaam, so that should work right? It doesn't though..
Because $filepath contains the parent folder of the file, which you cant delete obviously.
Not entirely true, the above script seems to do that just fine.

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.