怎么删除所有文件除了文件夹中的一个与PHP?

时间:2014-07-23 07:39:03

标签: php image file

我有这个PHP代码删除文件夹中的所有图像文件,但我需要保留example.php文件。

我怎么样?

$dir = @opendir('../result_image/');
while (false !== ($file = @readdir($dir)))
{
    if ($file != '..' && $file != '.')
    {
    $upload[] = $file;
    }
}

$time_sec=@time();
    for($i=0;$i<count($upload);$i++)
    {
    $time_file=@filemtime('../result_image/'.$upload[$i]);
    $time=$time_sec-$time_file;
if($time>9000)
    {
        //if ($upload[] = "disk_save.php") continue;
        @unlink('../result_image/'.$upload[$i]);
      }
   }

我的问题有3个答案,所有问题都可以解决。但是他们的女巫最好吗?每次用户上传图片时都会执行此代码。

5 个答案:

答案 0 :(得分:1)

我会这样做。

$time_sec=time();
$excludeFiles = array('example.php','disk_save.php');
for($i=0;$i<count($upload);$i++)
{
    $time_file=filemtime('../result_image/'.$upload[$i]);
    $time=$time_sec-$time_file;
    if($time>9000)
    {
        try {
            if (!in_array($upload[$i], $excludeFiles))
                unlink('../result_image/'.$upload[$i]);
        } catch (Exception $e) {
            // Error handle here
        }
    }
}

答案 1 :(得分:0)

您可以通过在if函数之前添加一个简单的unlink条件来完成此操作。

//Your code here    
if($upload[$i] != 'example.php'){
    unlink('../result_image/'.$upload[$i]);
}

注意:在函数前面添加@并不是一个好的做法,尝试克服所有错误。

答案 2 :(得分:0)

分解存储在上传中的每个值。我想你有文件的全名:

$unwantedExt = array('php');
for($i=0;$i<count($upload);$i++) {
    list($fileName, $extension) = explode('.', $upload[$i]);
    if(!in_array($extension, $unwantedExt)) {
       unlink('../result_image/'.$upload[$i]);
    }
}

答案 3 :(得分:0)

您可以使用glob()遍历目标目录中的所有文件。此外,如果您只想删除(例如) *.png文件,则可以将扩展程序添加到glob()调用以提高效率(以及确保您只删除您提到的图像文件)


    <?php

    $excluded = array('example.php', 'disk_save.php', 'some-other-file.php', ); //files to skip over, see comment below

    //if all of the images you want to delete have the same extension, it would be wise to
    //limit your iteration to only files with that extension:
    //  glob("../result_image/*.png")
    //If you want to delete multiple extensions:
    //  glob("../result_image/*.{png,jpg}", GLOB_BRACE)
    //Additionally, the `if (in_array(... continue;` statement becomes unnecessary and can be commented 
    //out or removed (assuming you added an extension to glob()). 
    foreach(glob("../result_image/*") as $fn) {
      if (in_array(basename($fn), $excluded)) //skip over excluded files
        continue; //see comment above

      $time_sec = time(); //if you are not working with a large number of image files, this line should be
                          //moved out of the foreach loop, to reduce execution time.
      $time_file = filemtime($fn);

      //check age of $fn and remove if it is old enough to be deleted
      if ($time_sec - $time_file > 9000) {
        unlink($fn);
      }
    }

答案 4 :(得分:0)

此代码也适用于我, 我将.php名称文件放在 IF 中,然后将另一个文件放入$ upload []。像这样:

$dir = opendir('../result_image/');
while (false !== ($file = readdir($dir)))
{
    if ($file != 'disk_save.php')
    {
    $upload[] = $file;
    }
}
$time_sec=time();
for($i=0;$i<count($upload);$i++)
{
$time_file=filemtime('../result_image/'.$upload[$i]);
$time=$time_sec-$time_file;
    if($time>9000)
    {
        //if($upload[$i] != 'disk_save.php'){
        unlink('../result_image/'.$upload[$i]);
        //}
    }
}