TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-11-2008, 09:40 AM   #1 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 18
Thanks: 0
CHASE is on a distinguished road
Smile 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'); 
__________________
World domination would be much easier, if you would all just surrender to me already.
Send a message via AIM to CHASE Send a message via MSN to CHASE Send a message via Skype™ to CHASE
CHASE is offline  
Reply With Quote
Old 03-11-2008, 10:49 AM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

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.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-11-2008, 10:51 AM   #3 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 18
Thanks: 0
CHASE is on a distinguished road
Default

I don't really see how that code is "better"..? It's pretty much the same thing..
__________________
World domination would be much easier, if you would all just surrender to me already.
Send a message via AIM to CHASE Send a message via MSN to CHASE Send a message via Skype™ to CHASE
CHASE is offline  
Reply With Quote
Old 03-11-2008, 10:53 AM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by CHASE View Post
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
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-11-2008, 10:57 AM   #5 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 18
Thanks: 0
CHASE is on a distinguished road
Default

As opposed to two ;) Not much of a difference, but thank you for the pointer :)
__________________
World domination would be much easier, if you would all just surrender to me already.
Send a message via AIM to CHASE Send a message via MSN to CHASE Send a message via Skype™ to CHASE
CHASE is offline  
Reply With Quote
Old 03-11-2008, 10:59 AM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by CHASE View Post
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
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-11-2008, 11:06 AM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Just a friendly tip, check out the glob function
__________________
Tanax is offline  
Reply With Quote
Old 03-11-2008, 11:08 AM   #8 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
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
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-11-2008, 03:14 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
Wildhoney is on a distinguished road
Default

Glob! I just fell in love with the glob function as soon as I stumbled across it.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 03-11-2008, 10:41 PM   #10 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 18
Thanks: 0
CHASE is on a distinguished road
Default

Glob will only return files of a certain extension, and supports only two arguments though.. This does an entire directory + sub directories :D
__________________
World domination would be much easier, if you would all just surrender to me already.
Send a message via AIM to CHASE Send a message via MSN to CHASE Send a message via Skype™ to CHASE
CHASE is offline  
Reply With Quote
Old 03-11-2008, 11:16 PM   #11 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

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 :)
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 03-12-2008, 02:29 AM   #12 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 18
Thanks: 0
CHASE is on a distinguished road
Default

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 :)
__________________
World domination would be much easier, if you would all just surrender to me already.
Send a message via AIM to CHASE Send a message via MSN to CHASE Send a message via Skype™ to CHASE
CHASE is offline  
Reply With Quote
Old 03-12-2008, 09:58 AM   #13 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Not to be a stickler but ++$var is faster then $var++
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 03-12-2008, 10:43 AM   #14 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 18
Thanks: 0
CHASE is on a distinguished road
Default

Oh cool, I didn't know that, thanks for the tip.
__________________
World domination would be much easier, if you would all just surrender to me already.
Send a message via AIM to CHASE Send a message via MSN to CHASE Send a message via Skype™ to CHASE
CHASE is offline  
Reply With Quote
Old 03-12-2008, 02:15 PM   #15 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by TlcAndres View Post
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 View Post
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.
Salathe is offline  
Reply With Quote
Old 03-12-2008, 04:08 PM   #16 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

Quote:
Originally Posted by CHASE View Post
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...
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design