我正在尝试删除子文件夹中具有相同父文件名的所有文件,然后删除父文件。
例如:
/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 :(得分: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.';
答案 1 :(得分:0)
修复了我没有在路径中定义正确变量的问题。