2014-02-21 78 views
0

我想刪除具有相同父文件名的子文件夾中的所有文件,然後刪除父文件。刪除具有相同文件名的子目錄中的所有文件

因此,例如:

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

你有什麼錯誤,如果有的話? – Sacha

+0

警告: – neoszion

回答

0

ü可以使用一些這樣的事

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

中沒有這樣的文件或目錄我認爲我需要遍歷所有仍然使用此功能的文件夾? – neoszion

0

修正我沒有定義路徑正確的變量的問題。

相關問題