SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If file extension exists

    I'm not sure which way to go or even how to write what I need. Should I use 'opendir' or 'glob', I want to open a certain file when another file doesn't exist. I want to search a main directory and certain 2nd level sub directories, not all the 2nd level sub directories though, and not any 3rd level sub directories. Basically if "file_extn A = true" then continue search, if "file_extn A = false" then echo "file_extn B". I've uploaded a sample image to try and give a better idea. The blue cells indicate the directories I want to search, the yellow cells are directories I want to exclude from the search and the green cell is the file I want to echo, if anybody can help it would be greatly appreciated.

    Diectory tree.jpg

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try with DirectoryIterator().
    Here's an example, not sure if it works, but it is an ideea to start to create your script.
    The ideea is to make an array with directories in which to search, traverse the array and get info about each directory.
    A tutorial about DirectoryIterator(), with more examples, you can find on: DirectoryIterator to get file and directory info.
    PHP Code:
    $dirtosearch = array('dir''dir/a''dir/b');
    $nrdir count($dirtosearch);
    for(
    $i=0$i<$nrdir$i++) {
    $objDI = new DirectoryIterator($dirtosearch[$i]);
      foreach(
    $objDI as $fileinfo) {
        
    // if it's a file
        
    if($fileinfo->isFile()) {
          if(
    $fileinfo->getExtension() == 'file_extn A') {
            echo 
    'file_extn B';
            break 
    2;
          }
        }
      }

    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers MarPlo I'll give it a try and let you know, thanx for the link also, mate

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I made an error in my original post. I want to post or get 'file_extn B's actual file, not echo the extn. Otherwise this code may work. Sorry for the misunderstanding.
    As 'file_extn B's actual file creates 'file extn A's actual file (after user input), I was also wondering if it can be deleted after say a 24 hour period by implementing extra code in the code that creates it. Cheers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •