Check multiple folders for a file?

Discussion in 'PHP' started by MrLeN, Feb 21, 2011.

  1. #1
    If I have multiple folders with files in each, ie:

    folder_1
    banana.txt
    apple.txt

    folder_2
    peach.txt
    strawberry.txt

    folder_3
    plum.txt
    coconut.txt

    Now lets say I am adding banana.txt to folder_3. I don't want it in folder 1 anymore.

    So what I want to do is create a file called banana.txt in folder_3 and then have some kind of loop that will go through all the folders and delete banana.txt from all folders (except, of course, folder_3)

    Here's what I have so far:

    
    $filename = "banana.txt";
    
    if (file_exists($filename)) {
      unlink($myFile);
    fclose($fh);
    
    } else {
        //
    }
    
    Code (markup):
    That's what I want to do, but I realise I have to put that code inside some sort of loop which opens all the folders - but I I don't know how.
     
    MrLeN, Feb 21, 2011 IP
  2. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #2
    This is the code I am going to use if all else fails. I will have to make 23 instances of it. Here's an example of 2 of them:

    
    			//==see if file is in another category and delete it
    			$file_1  = $_SERVER['DOCUMENT_ROOT'] . "/categories/1/".$youtube_username.".txt";
    			if ((file_exists($file_1)) && ($file_1 != $file_new) ) {
            unlink($myFile);
          }
    			$file_2  = $_SERVER['DOCUMENT_ROOT'] . "/categories/2/".$youtube_username.".txt";
    			if ((file_exists($file_1)) && ($file_1 != $file_new) ) {
            unlink($myFile);
          }
    			//==end see if file is in another category and delete it
    
    Code (markup):
     
    MrLeN, Feb 21, 2011 IP
  3. ap2010

    ap2010 Guest

    Messages:
    41
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There are dozen ways to do this in PHP. I suggest that you look at the glob function or the dir class (you can find the references on php manual website).
     
    ap2010, Feb 21, 2011 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Quickie:
    If folders are always have numeric pattern in

    
    $total_folders = 3;
    $file_to_delete = 'banana.txt';
    
    for ($i=1; $i<=$total_folders; $i++)
          unlink( "$_SERVER[DOCUMENT_ROOT]/categories/$i/$file_to_delete" );
    
    Code (markup):

    I hope it helps.
     
    Vooler, Feb 21, 2011 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    
    
    // List of all the folders
    $folder_list = array('folder_1', 'new_folder_2',' folder_2');
    
    // List of files (key - filename, value - foldername)
    $file_add = array('banana.txt' => 'folder_1', 
                             'apples.txt' => 'folder_2');
    
    // Loop through all files
    foreach($file_add as $file_name => $folder_name)
    {
         // Loop through all folders
         foreach($folder_list as $this_folder)
         {
               // If file exists in this folder and this folder is not the copy to folder
               if(is_file($this_folder . '/' . $file_name) && $this_folder != $folder_name) {
                          // If file hasn't been copied over to the copy to folder, move it
                          if(!is_file($folder_name . '/' . $file_name)) {
                                     rename($this_folder . '/' . $file_name, $folder_name . '/'. $file_name);
                          } else {
                          // Otherwise remove it
                                     unlink($this_folder . '/' . $file_name);
                          }
               }
         }
    }
    
    PHP:
    Hope it helps..
     
    ThePHPMaster, Feb 21, 2011 IP
  6. ap2010

    ap2010 Guest

    Messages:
    41
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    In my previous answer I suggested using glob or dir. Here is an example of how you would use the glob function:

    <?php
    // -- wild card ----------------------------------------------------------------------------------v
    $search = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "categories" . DIRECTORY_SEPARATOR . "*" . DIRECTORY_SEPARATOR . $youtube_username . ".txt";
    $ignore = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "categories" . DIRECTORY_SEPARATOR . "1" . DIRECTORY_SEPARATOR . $youtube_username . ".txt";
    // -- the folder to ignore -----------------------------------------------------------------------^
    $files = glob($search);
    foreach($files as $file) {
        // we perform realpath() on paths so that the character-case and slashes of both paths become consistent
        if (realpath($file) != realpath($ignore)) {
            // unlink($file);
            echo "unlink($file)<br>";
        }
    }
    ?>
    
    PHP:
     
    Last edited: Feb 21, 2011
    ap2010, Feb 21, 2011 IP
  7. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    use an if statment to check if the file exists in the folder if so delete it if not check the next with an else if statement following it
     
    srisen2, Feb 22, 2011 IP