简体   繁体   中英

Delete all files in subdirectories with the same filename

I am trying to remove all files which are in subfolders with the same parent filename then delete the parent.

So for example:

/assets/pages/1/filename.jpg
/assets/pages/1/100x100/filename.jpg
/assets/pages/1/250x250/filename.jpg

here is my current php but its not working I keep getting file not found even though the path is correct!

<?php
$imgtype            = 'pages';
$file_types         = array('.jpg', '.jpeg', '.png', '.gif');
$image_file_path    = DIRECTORY_SEPARATOR . 'www' . DIRECTORY_SEPARATOR . 'assets' .  DIRECTORY_SEPARATOR . $imgtype;

if (!is_dir($image_file_path))
{
    try
    {
        mkdir($image_file_path);
    }
    catch (Exception $e)
    {
        return '';
    }
}

$image_file_path    .= DIRECTORY_SEPARATOR . $folder_id . DIRECTORY_SEPARATOR;
$folders            = array_filter(glob($image_file_path . '*'), 'is_dir');

foreach ($folders as $folderi)
{
    $files = array_filter(glob($folderi . DIRECTORY_SEPARATOR . 'filename.jpg' . "*"), 'is_file');

    foreach ($files as $file)
    {
        @unlink($folderi.DIRECTORY_SEPARATOR.$file);
    }
}
foreach ($file_types as $type)
{
    $files = array_filter(glob($image_file_path . 'filename.jpg' . "*" . $type), 'is_file');

    foreach ($files as $file)
    {
        @unlink($image_file_path . $file);
    }
}
?>

u can use some thing like this

>  define('PATH', '../');   function destroy($dir) {
>     $mydir = opendir($dir);
>     while(false !== ($file = readdir($mydir))) {   if($file != 'jQuery')   {
>         if($file != "." && $file != "..") {
>             chmod($dir.$file, 0777);
>             if(is_dir($dir.$file)) {
>                 chdir('.');
>                 destroy($dir.$file.'/');
>                 rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");
>             }
>             else
>                 unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
>         }         }
>     }
>     closedir($mydir); } destroy(PATH); echo 'all done.';  

修复了我未在路径中定义正确变量的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM