簡體   English   中英

刪除子目錄中具有相同文件名的所有文件

[英]Delete all files in subdirectories with the same filename

我正在嘗試刪除具有相同父文件名的子文件夾中的所有文件,然后刪除父文件。

因此,例如:

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

這是我當前的php,但是它不起作用,即使路徑正確,我也一直找不到文件!

<?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);
    }
}
?>

你可以用這樣的東西

>  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.';  

修復了我未在路徑中定義正確變量的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM