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 06-10-2005, 02:55 AM   PM User | #1
Kura_kai
Regular Coder

 
Join Date: Dec 2004
Posts: 202
Thanks: 0
Thanked 0 Times in 0 Posts
Kura_kai is an unknown quantity at this point
Php Help-Getting files

what commands will return all the files of a directory in a array. Or something along that line.
Kura_kai is offline   Reply With Quote
Old 06-10-2005, 03:49 AM   PM User | #2
CrAzY_J
Regular Coder

 
Join Date: May 2005
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
CrAzY_J is an unknown quantity at this point
PHP Code:
<?php
$save_in_array
=file('my_file.dat');
?>
CrAzY_J is offline   Reply With Quote
Old 06-10-2005, 10:04 PM   PM User | #3
Kura_kai
Regular Coder

 
Join Date: Dec 2004
Posts: 202
Thanks: 0
Thanked 0 Times in 0 Posts
Kura_kai is an unknown quantity at this point
No not a file but all files in a folder
Kura_kai is offline   Reply With Quote
Old 06-10-2005, 11:44 PM   PM User | #4
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
PHP Code:
<?php

function arrFiles ($dir)
{
    
$handle opendir($dir);

    while (
$file readdir($handle))
    {
        if (
$file != '.' && $file != '..')
        {
            
$fileArr[] = $file;
        }
    }

    
closedir($handle);
    return 
$fileArr;
}

$f arrFiles("C:\\");

foreach(
$f as $allFiles)
{
    echo 
$allFiles "<br>";
}

?>
Could be better...
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 06-11-2005, 03:13 AM   PM User | #5
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
Just a couple of things with that. Firstly, I don't know how it works on IIS, but on a *nix machine directories are files, too. Therefore the function returns not only files in the directory but sub directories too (which is fine if that's what you want, but just be aware that it's happening). Also, due to the way while loops work you will always receive one empty array on the end which serves no purpose.

Here's a slight re-working which strips out all directories and empty arrays and leaves you just the filenames:

PHP Code:
function get_filenames($dir='.') {
    if (
$handle opendir($dir)) {
        while (
false !== ($file readdir($handle))) {
            
$check_file = (!empty($file) && !is_dir($file) ? $file '');
            if(!empty(
$check_file)) {
                
$return_file[] = $check_file;
            }
        }
        
closedir($handle);
        return 
$return_file;        
    }
}

print_r(get_filenames()); 
__________________
~ Bazzy
delinear 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 10:26 AM.


Advertisement
Log in to turn off these ads.