TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Script Giveaway (http://www.talkphp.com/script-giveaway/)
-   -   Read Directory Function (http://www.talkphp.com/script-giveaway/2455-read-directory-function.html)

CHASE 03-11-2008 09:40 AM

Read Directory Function
 
Most of you developing websites or having portfolios, this function may come in use in your PHP endeavors.

Example: Directory Quick Browse Script

Added Note: If you're using anything less than php version 5.0.2, or you're getting an error using the code, add this in as well.
PHP Code:

if(!defined('PHP_EOL'))
{
    
define('PHP_EOL', (strtoupper(substr(PHP_OS03)) === 'WIN' "\r\n" "\n"));


And here's the function :)
PHP Code:

function read_directory($directory)
{
    if(
is_dir($directory))
    {
        echo 
"<ul>"  PHP_EOL;
        
        if(
$handle = @opendir($directory))
        {                    
            while((
$file readdir($handle)) !== false)
            {
                if(
$file != "." && $file != "..")
                {
                    if(
is_dir($directory DIRECTORY_SEPARATOR $file))
                    {
                        
$directories[] = $file;
                    }
                    else
                    {
                        
$files[] = $file;
                    }
                }
            }
            
            if(!empty(
$directories))
            {
                
sort($directories);
                
                foreach(
$directories as $sub_directory)
                {
                    echo 
"<li>" $sub_directory DIRECTORY_SEPARATOR "</li>";
                    echo 
"<ul>";
                    
                    
read_directory($directory DIRECTORY_SEPARATOR $sub_directory);
                    
                    echo 
"</ul>";
                }
            }
            
            if(!empty(
$files))
            {
                
sort($files);
                
                foreach(
$files as $file)
                {
                    echo 
"<li><a href=\"" $directory DIRECTORY_SEPARATOR $file "\">" $file "</a></li>"  PHP_EOL;
                }
            }
            
            @
closedir($handle);
            echo 
"</ul>"  PHP_EOL;
        }
    }


To use it, just simply write the following code:
PHP Code:

read_directory('portfolio'); 


Orc 03-11-2008 10:49 AM

Theres a better way to escape the two periods
PHP Code:


if ($file != ".") continue; 

Only problem is you cannot do an else/elseif statement for that condition.

CHASE 03-11-2008 10:51 AM

I don't really see how that code is "better"..? It's pretty much the same thing..

Orc 03-11-2008 10:53 AM

Quote:

Originally Posted by CHASE (Post 12236)
I don't really see how that code is "better"..? It's pretty much the same thing..

It's only one line of code xD

CHASE 03-11-2008 10:57 AM

As opposed to two ;) Not much of a difference, but thank you for the pointer :)

Orc 03-11-2008 10:59 AM

Quote:

Originally Posted by CHASE (Post 12238)
As opposed to two ;) Not much of a difference, but thank you for the pointer :)

No problem, just trying to help. ^_^ and be a little snobby at the same time :-D

Tanax 03-11-2008 11:06 AM

Just a friendly tip, check out the glob function :-)

Orc 03-11-2008 11:08 AM

Quote:

Originally Posted by Tanax (Post 12240)
Just a friendly tip, check out the glob function :-)

I was thinking about that, but I didn't bother posting it xD at least, I thought Wildhoney >.> was going to post hehe

Wildhoney 03-11-2008 03:14 PM

Glob! I just fell in love with the glob function as soon as I stumbled across it.

CHASE 03-11-2008 10:41 PM

Glob will only return files of a certain extension, and supports only two arguments though.. This does an entire directory + sub directories :D

freenity 03-11-2008 11:16 PM

didn't want to create a new thread because the topic is really similar.
I was trying to make a recursive function just to count how many files are in a dir and it's subdirectories:

PHP Code:

<?php

$dir 
"/var/www/html/";


$files getfilesnum($dir);


echo 
$files." files in {$dir} and subdirs<br />";

closedir($dh);


function 
getfilesnum($d)
{
    
$dh opendir($d);
    
$files 0;
    while (
$f readdir($dh))
    {
        if (
filetype($d.$f) == 'file')
        {
            
$files++;
        }
        elseif ((
filetype($d.$f) == 'dir') && ($f != '.') && ($f != '..'))
        {
            
$files += getfilesnum($d.$f);
        }
    }
    return 
$files;
}

?>

Isn't working :S it doesn't go recursive, don't know why. Any idea??

I saw that CHASE in his code put all the directories in a array and then called the recursive function, but it should work without using the array I think...

Aah I posted twice, please delete it admins :)

CHASE 03-12-2008 02:29 AM

I would recommend using the function I built, and then just removing the echos, and having it $var++

That would probably do what you want :)

TlcAndres 03-12-2008 09:58 AM

Not to be a stickler but ++$var is faster then $var++

CHASE 03-12-2008 10:43 AM

Oh cool, I didn't know that, thanks for the tip.

Salathe 03-12-2008 02:15 PM

Quote:

Originally Posted by TlcAndres (Post 12299)
Not to be a stickler but ++$var is faster then $var++

By approximately 0.00000004 seconds each time. I'd say it's a moot point in this particular case when accessing the file system is the slowest operation occurring.

Quote:

Originally Posted by CHASE (Post 12285)
Glob will only return files of a certain extension, and supports only two arguments though.. This does an entire directory + sub directories :D

glob will return whatever you tell it to return. If you just specify * as the pattern, glob will return all files and directories in the current working directory. It's simple enough to check for directories and go through each recursively, as you do in your own code.

freenity 03-12-2008 04:08 PM

Quote:

Originally Posted by CHASE (Post 12296)
I would recommend using the function I built, and then just removing the echos, and having it $var++

That would probably do what you want :)

yes, but I want to use my code :)
Just want to know what is wrong there... ^^


All times are GMT. The time now is 08:37 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0