简体   繁体   English

使用PHP删除数组中的特定文件名

[英]Delete specific filenames in an array using PHP

I have a list of PDFs and I would like to delete them. 我有一份PDF清单,我想删除它们。 Can someone please explain to a beginner how this is done? 有人可以告诉初学者如何做到这一点吗? At this point, I can do this in Excel a number of different ways but how can I simply do this in PHP? 在这一点上,我可以通过多种方法在Excel中执行此操作,但是如何在PHP中简单地执行此操作呢?

Most simple solution: 最简单的解决方案:

<?php

    $dir = '/files_directory/';
    $files_to_delete = array('file1.pdf', 'file3.pdf', 'file4.pdf');

    foreach($files_to_delete as $file)
    {
        $file_path = $dir . $file;
        if(is_file($file_path))
        {
            unlink($file_path);
        }
    }

Use PHP to search for the folder, do a loop through the files there and add some conditional statements to the loop. 使用PHP搜索文件夹,在其中的文件之间进行循环,并在循环中添加一些条件语句。 If they are true or false, act on those results. 如果它们是对还是错,请对那些结果采取行动。

As you have not typed any code for us, I am not going to type any code back to you. 由于您尚未为我们键入任何代码,因此我不会再给您键入任何代码。

The most simple way I could think of doing it: 我想到的最简单的方法是:

array_map('unlink', $array_with_files_to_delete);

$array_with_files_to_keep = array_diff($list_with_pdfs, $array_with_files_to_delete);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM