2016-12-14 72 views
-2
中的一些文件

我有一个脚本需要删除某些以某种模式开始的文件。删除php

我试过unlink,但不知何故没有工作,有什么是缺少的。

$files = glob(PATH_DIR. 
    '_*.txt'); 
foreach($files as $file) { // iterate files 
    if (is_file($file)) { 
     //unlink($file); // delete file 
     $mask = $var. 
     '_*.*'; 
     array_map('unlink', glob($mask)); 
    } 
} 
+0

你为什么这样做'array_map()'一个循环内?你在寻找哪种模式,'_ *。txt'或'_ *。*'?什么是'$ var'?你没有在显示的代码中定义它,但是尝试在'$ mask'中使用它,这是你调用'unlink()'的基础。 –

回答

0
@array_map('unlink', glob(PATH_DIR.'_*.txt')); 

_-如果不打印所有路径,然后尝试manualy触发取消链接

+0

'$ mask'怎么样,我需要删除以某种模式开始的文件。 – Mann

+0

这就是为什么你删除了所有/_222.txt文件,尝试使用die来代替glob,然后你会看到什么是不正确的,否则许可问题 – 2016-12-14 05:29:28

+0

请不要使用'@'来压制错误。这是非常糟糕的做法。 –

0

使用RecursiveIteratorIterator用于将文件复制,并检查你有足够的权限删除这些文件。无论chown该文件夹或更改权限755和文件644

find . -type d -exec chmod 0755 {} $path; 
find . -type f -exec chmod 0644 {} $path; 

这些命令将有助于递归设置权限,如果你是在Linux上

$file_obj = get_files_by_path('/var/www/html/wordpress') 

foreach ($file_obj as $Ofiles) { 
    $file_path = $Ofiles->getPathname(); 
    //use your regex here with $file path. if that passed then delete the file 

    unlink($file_path); 
} 

function get_files_obj_by_path($path){ 
      $obj = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD 
       ); 
      return $obj; 
     }