Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2010, 12:03 AM   PM User | #1
rootmath
New Coder

 
Join Date: Oct 2010
Posts: 29
Thanks: 6
Thanked 0 Times in 0 Posts
rootmath is an unknown quantity at this point
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..
rootmath is offline   Reply With Quote
Old 12-04-2010, 12:10 AM   PM User | #2
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
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
Lamped is offline   Reply With Quote
Users who have thanked Lamped for this post:
rootmath (12-04-2010)
Old 12-04-2010, 12:36 AM   PM User | #3
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,225
Thanks: 11
Thanked 154 Times in 154 Posts
DrDOS is infamous around these parts
The key phrase here is 'folder tree', Google that plus 'php' and you will find plenty of results for multilevel recursive folder trees.
DrDOS is offline   Reply With Quote
Old 12-04-2010, 01:14 AM   PM User | #4
rootmath
New Coder

 
Join Date: Oct 2010
Posts: 29
Thanks: 6
Thanked 0 Times in 0 Posts
rootmath is an unknown quantity at this point
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)


Quote:
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
rootmath is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:07 AM.


Advertisement
Log in to turn off these ads.