Show DevBest [PHP] Simple File Directory Scanner

Status
Not open for further replies.

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
I've been wanting to do one of these for some time as I find them very handy.

Basically, this function I've created allows you to get files from any directory on your webserver.

The code can be found below:
PHP:
<?php
function GetFiles( $directory, $output_array = false, $exts = array( "png", "jpeg", "jpg", "bmp", "gif" ) )
{
    if ( is_dir( $directory ) )
    {
        $scan   = scandir( $directory );
        $return = array();
        foreach( $scan as $file )
        {
            $ext = strtolower( end( explode( ".", $file ) ) );
            if ( $file !== "." && $file !== ".." & in_array( $ext, $exts ) == true )
            {
                if ( $output_array == true )
                {
                    $return[] = $file;
                }
                else
                {
                    echo $file . "<br />";
                }
            }
        }
        if ( $output_array == true ) return $return;
    }
    else
    {
        echo "Directory {$directory} is invalid!";
    }
}
 
GetFiles( "images/work" );
?>
The code above will return something similar to this:
1322077415-Screenshot_1.png


PHP:
<?php
function GetFiles( $directory, $output_array = false, $exts = array( "png", "jpeg", "jpg", "bmp", "gif" ) )
{
    if ( is_dir( $directory ) )
    {
        $scan   = scandir( $directory );
        $return = array();
        foreach( $scan as $file )
        {
            $ext = strtolower( end( explode( ".", $file ) ) );
            if ( $file !== "." && $file !== ".." & in_array( $ext, $exts ) == true )
            {
                if ( $output_array == true )
                {
                    $return[] = $file;
                }
                else
                {
                    echo $file . "<br />";
                }
            }
        }
        if ( $output_array == true ) return $return;
    }
    else
    {
        echo "Directory {$directory} is invalid!";
    }
}
 
echo "<pre>";
print_r ( GetFiles( "images/work", true ) );
echo "</pre>";
?>
The code above will return something similar to:
1322077415-Screenshot_4.png


You can make the function return an array by adding ", true" after your directory.

Hope this script helps.

m0nsta.
 

At0m

Active Member
Sep 10, 2011
101
40
Wow, nice code m0nsta, great job :D
This will help me to undestand more PHP.
Keep going.
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Nice, nice!

I know another way of doing something like that ;P

PHP:
function getFiles($dir)
{
    foreach(glob($dir) as $file)
    {
          echo $file; //Just echo it, edit it and make it do whatever you want
    }
}

So you could do...

PHP:
getFiles('images/*.png'); //Get all PNG files in the directory
getFiles('images/*.jpg'); //Get all JPG files in the directory
getFiles('images/*'); //Get all files in the directory
getFiles('images/*.txt'); //Get all TXT files in the directory

Should work, I probably have a typo there. Me + DevBest Post Form + Programming === typo/bad
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Nice, I didn't know you could do that - you learn something new everyday!

And as for typos, yes -- you put 'funtion' and not 'function' ;)
 

Sledmore

Chaturbate Livestreamer
Staff member
FindRetros Moderator
Jul 24, 2010
5,198
3,914
You don't know how much I love you for this, I tried something ages ago for my roleplay hotel, like this, but the code was a little crap xD, I'll try this out thanks for it, ;D.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
You don't know how much I love you for this, I tried something ages ago for my roleplay hotel, like this, but the code was a little crap xD, I'll try this out thanks for it, ;D.
No worries Sledmore :D!!
 
Status
Not open for further replies.

Users who are viewing this thread

Top