0

I understand how to delete a specified file using the 'unlink' command in PHP, but what I need to do is to write some code that will take all (10,000+) files in a folder (possibly put them into an array), open them, and then delete ONLY files that contain specific information. (All files in the folder are .txt files containing a list, or array, of numbers. EX: I want to delete any files where 4,5,6,7th slots in the array contained 20,20,100,100 respectively).

Is this a start:

<?php
$directory = '/path/to/files';

if (! is_dir($directory)) {
    exit('Invalid diretory path');
}

$files = array();

foreach (scandir($directory) as $file) {
    if ('.' === $file) continue;
    if ('..' === $file) continue;

    $files[] = $file;
}

var_dump($files);
?>
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
Brian King
  • 21
  • 5
  • 1
    so you seem to be getting a list of all the files in the directory. Have you tried to open any of them and read them in? You should create a function to read through each $file in $files to get you along your way. – Shawn Mehan Dec 18 '15 at 19:48
  • 3
    instead of putting file into array, start fetching contents using fread( or file_get_contents if the file size is not so big), match the condition, and start deleting the files. – Jigar Dec 18 '15 at 19:52
  • This might be useful for opening and reading file contents http://stackoverflow.com/questions/33850820/parse-txt-files-and-turn-them-into-static-html-files/33851340#33851340 – Steve Dec 18 '15 at 23:41

1 Answers1

0

Following on from @ShawnMehan 's and @Jigar 's comments and from my own - I have not been able to test this where I am but it might serve to point you in a direction for what you need. Not sure how many rows of arrays you have in each file, so I have used a loop but if there is only one line, you will only need the fgets().

I have assumed that your arrays in the text files are comma delimited, but if they are tab delimited you would need to explode them by "\t".

       $files = array();
       $each_line = array();
       $file_counter = 0;
       $directory = '/path/to/files';

       if (! is_dir($directory)) {
       exit('Invalid directory path');
       }

       $files = glob($directory."/*.txt"); // Scan directory for .txt files

       // Check that there are .txt files in directory
       if ($files !== false) {
       $number_of_files = count($files); // Count number of .txt files in directory
          while($file_counter < $number_of_files){
          $file_handle = fopen ($files[$file_counter], "r"); // Open file
                   while (!feof ($file_handle)) {  
                   // get the arrays a row at a time and put each row into array
                   $each_line = explode( ',', fgets($file_handle, 1000)); 
                        if($each_line[3] == 20 || $each_line[4] == 20 || $each_line [5] == 100 || $each_line[6] == 100){
                        unlink($file_handle);
                        }
                   }
               if(file_exists($file_handle)){
               fclose($file_handle);
               }
           $file_counter++;   
           }
       }else{
       echo "No text files found in this directory";
       }

Some notes on the use of glob(); for opening files in case you may not be opening local files (it won't work on remote files): http://php.net/manual/en/function.glob.php

Steve
  • 808
  • 1
  • 9
  • 14