Frage

$value can = a folder structure to the language file. Example: languages/english.php

$value can also = the files name. Example: english.php

So I need to get the current folder that $value is in and delete the folder ONLY if there are no other files/folders within that directory (after deleting the actual file as I am doing already, ofcourse).

foreach($module['languages'] as $lang => $langFile)
{
        foreach ($langFile as $type => $value)
        {
            @unlink($module_path . '/' . $value);
            // Now I need to delete the folder ONLY if there are no other directories inside the folder where it is currently at.
            // And ONLY if there are NO OTHER files within that folder also.
        }
}

How can I do this?? And wondering if this can be done without using a while loop, since a while loop within a foreach loop could take some time, and need this to be as quick as possible.

And just FYI, the $module_path should never be deleted. So if $value = english.php, it should never delete the $module_path. Ofcourse, there will always be another file in there, so checking for this is not necessary, but won't hurt either way.

Thanks guys :)

EDIT

Ok, now I'm using this code here and it is NOT working, it is not removing the folders or the files, and I don't get any errors either... so not sure what the problem is here:

foreach($module['languages'] as $lang => $langFile)
{
    foreach ($langFile as $type => $value)
    {
        if (@unlink($module_path . '/' . $value))
            @rmdir(dirname($module_path . '/' . $value));
    }
}

NEVERMIND, this works a CHARM!!! Cheers Everyone!!

War es hilfreich?

Lösung

Since the directory you care about might be part of the $value, you need to use dirname to figure out what the parent directory is, you can't just assume that it's $module_path.

$file_path = $module_path . '/' . $value;

if (@unlink($file_path)) {
    @rmdir(dirname($file_path));
}

Andere Tipps

The easyest way is try to use rmdir. This don't delete folder if it is not empty

rmdir($module_path);

also you can check is folder empty by

if(count(glob($module_path.'*'))<3)//delete

2 for . and ..

UPD: as I reviewed maybe you should replace $module_path by dirname($module_path.'.'.$value);

if (is_file($value)) { 
  unlink($value); 

} else if (is_dir($value)) {
  if  (count(scandir($value)) == 2) }
     unlink($value)  
  }
}

http://php.net/manual/en/function.is-dir.php

http://www.php.net/manual/en/function.scandir.php

The code below will take a path, check if it is a file (i.e. not a directory). If it is a file, it will extract the directory name, then delete the file, then iterate over the dir and count the files in it, if the files are zero it'll delete the dir.

Code is as an example and should work, however privileges and environment setup may result in it not working.

<?php

if(!is_dir ( string $filename )){ //if it is a file
    $fileDir = dirname ( $filename );
    if ($handle = opendir($fileDir)) {
        echo "Directory handle: $handle\n";
        echo "Files:\n";
        $numFiles=0;

        //delete the file
        unlink($myFile);

        //Loop the dir and count the file in it
        while (false !== ($file = readdir($handle))) {
            $numFiles = $numFiles + 1;
        }

        if($numFiles == 0) { 
            //delete the dir
            rmdir($fileDir);
        }

        closedir($handle);
    }
}
?>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top