Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Array from filenames

    I need to choose a directory. Run through its sub-directories, and populate an array with file names, on the condition that the files start with a number. If I have, for instance,

    Chapter1
    |_ Lesson1
    | |_ 1.1-limits.php
    | |_ images
    |
    |_ Lesson 2
    |_ 1.2-delta-epsilon.php

    etc.

    I need to look through chapter 1, go in through all the lessons and return an array of 1.1-limits.php, 1.2-delta-epsilon.php

    I thought about using a loop to go into each lesson folder because they are sequentially numbered, but im not sure how to get the file out and store it in an array.

    Thanks!
    Last edited by rootmath; 12-04-2010 at 12:38 AM.

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    function filesToArray($folder, &$result) {
        
    $files scandir($folder);
        foreach(
    $files as $file) {
            if (
    is_dir($folder.'/'.$file) && ($file != '.') && ($file != '..')) {
                
    filesToArray($folder.'/'.$file$result);
            } elseif (
    is_file($folder.'/'.$file) && (preg_match('/^[0-9]+/'$file)) {
                
    $result[] = $folder.'/'.$file;
            }
        }
    }

    $files = array();
    filesToArray('/myfolder'$files);
    var_dump($files); 
    Disclaimer: Totally untested, I'm tired, it might not work and I've used some lazy techniques. So there.

    Set the preg_match regex to check the filename is what you want. If you want to check for 1.1- (number, dot, number, dash) do: '/^[0-9]\.[0-9]\-/'
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • Users who have thanked Lamped for this post:

    rootmath (12-04-2010)

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,916
    Thanks
    15
    Thanked 227 Times in 227 Posts
    The key phrase here is 'folder tree', Google that plus 'php' and you will find plenty of results for multilevel recursive folder trees.

  • #4
    New Coder
    Join Date
    Oct 2010
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Lamped View Post
    PHP Code:
    function filesToArray($folder, &$result) {
        
    $files scandir($folder);
        foreach(
    $files as $file) {
            if (
    is_dir($folder.'/'.$file) && ($file != '.') && ($file != '..')) {
                
    filesToArray($folder.'/'.$file$result);
            } elseif (
    is_file($folder.'/'.$file) && (preg_match('/^[0-9]+/'$file)) {
                
    $result[] = $folder.'/'.$file;
            }
        }
    }

    $files = array();
    filesToArray('/myfolder'$files);
    var_dump($files); 
    Disclaimer: Totally untested, I'm tired, it might not work and I've used some lazy techniques. So there.

    Set the preg_match regex to check the filename is what you want. If you want to check for 1.1- (number, dot, number, dash) do: '/^[0-9]\.[0-9]\-/'
    Thanks I will give it a try and in particular it gave me a place to start looking up things I'm not familar with. For example I've never seen foreach (my coding background is limited to java, and I just started really working with php a fews days ago)


    The key phrase here is 'folder tree', Google that plus 'php' and you will find plenty of results for multilevel recursive folder trees.
    Thanks, I googled my way to recursive folder functions, but didn't really quite understand what was going on, I have some more reading and research to do. Thanks for pointing me in the right direction


  •  

    Posting Permissions

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