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 04-08-2009, 05:01 PM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application Array diplaying Folder on top ?

Good day,
here I'm working on an array which read a directory and list the files and folders recursively.

I'm trying to display in bold , and on top of all files, all the folders.


PHP Code:

<?php

$directory 
"Art/";
function 
dirList ($directory
{

    
// create an array to hold directory list
    
$results = array();

    
// create a handler for the directory
    
$handler opendir($directory);

    
// keep going until all files in directory have been read
    
while ($file readdir($handler)) {

        
// if $file isn't this directory or its parent, 
        // add it to the results array
        
if ($file != '.' && $file != '..')
        
        
// If file is directory, mark it in bold.
        
if (is_dir($file)){
        echo 
"<b>";
        echo 
$file;
        echo  
"</b><br/>";
        
        
// Else not styled
        
}else{
        echo 
$file;
        echo  
"<br/>";
        
    }
    }

// Here probably should be my code for sorting the folder on top of the files.


    // tidy up: close the handler
    
closedir($handler);

    
// done!
    
return $results;

}

dirList($directory);





?>
Thanks !
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 04-08-2009, 05:49 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Here, I fixed it for you. I tested this on my /etc/ntp/ directory so just change it to whatever yours is.


Code:
<?php

$directory = "/etc/ntp/";
function dirList ($directory) 
{

    $directory_link = "/etc/ntp/";
	
	// create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {

        $file = "$directory_link" . $file;
		
		$level1 = "$directory_link" . '.';
		$level2 = "$directory_link" . '..';
		
		// if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != "$level1" && $file != "$level2")
        
        // If file is directory, mark it in bold.
        if (is_dir($file) == TRUE){
        print "<b>$file</b><br/>";
        
        // Else not styled
        }else{
        print "$file<br/>";
        
    }
    }

// Here probably should be my code for sorting the folder on top of the files.


    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}

dirList($directory);





?>

/etc/ntp/ntp2
/etc/ntp/step-tickers
/etc/ntp/keys
/etc/ntp/ntpservers
allworknoplay is offline  
Reply With Quote
Old 04-08-2009, 07:40 PM   #3 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

Nice, it show the folder in bold, can I make the folder list first and then the files.
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 04-08-2009, 08:01 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Peuplarchie View Post
Nice, it show the folder in bold, can I make the folder list first and then the files.
I can't tell if you're asking a question or making a statement.

But the current code shows the folders first in bold and the files in regular format...
allworknoplay is offline  
Reply With Quote
Old 04-08-2009, 09:09 PM   #5 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

I do have a question for you.

You create this array to hold the data:

$results = array();


But I don't see anywhere in the function where you actually send the data to the $results array variable. Does PHP implicitly somehow do this behind the scenes?

Thanks..
allworknoplay is offline  
Reply With Quote
Old 04-08-2009, 11:32 PM   #6 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

I echo ed them before, yes, you are right.
Learning...
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 04-08-2009, 11:34 PM   #7 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

You are right, I have ech-ed them before so the is no point for this line I think now.
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 04-09-2009, 12:15 AM   #8 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

RESOLVED

PHP Code:

<?php

$directory 
"Art/SKYDOME/";
function 
dirList ($directory)
{

    
//create 2 arrays - one for folders and one for files
   
$folders = array();
   
$files = array();

    
// create a handler for the directory
    
$handler opendir($directory);

    
// keep going until all files in directory have been read
    
while ($file readdir($handler)) {

        
// if $file isn't this directory or its parent,
        // add it to the results array
        
if ($file != '.' && $file != '..')
        
        
// If file is directory, mark it in bold.
       
if(is_dir($directory.$file)) { 
        
array_push($folders,$file);
        
        
// Else not styled
        
}else{
        
array_push($files,$file);
        
    }
    }


    
// tidy up: close the handler
    
closedir($handler);

    foreach(
$folders as $folder) {
      echo 
"<strong>".$folder."</strong><br />";
    }

    foreach(
$files as $file) {
      echo 
$file."<br />";
    }
    

}

dirList($directory);

?>
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 04-09-2009, 12:17 AM   #9 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Looks much cleaner....

I know that my code wasn't clean earlier but I knew that you would get the point and clean it up....
allworknoplay is offline  
Reply With Quote
Old 04-09-2009, 09:37 AM   #10 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 13
Thanks: 0
Ultimatum is on a distinguished road
Default

I have a few comments on your code.
PHP Code:
while ($file readdir($handler)) { 
because readdir returns a bool, you can check if readdir succeeds or not
PHP Code:
while (false !== ($file readdir($handler))) { 
And this line isn't really usefull, cause you make the if statement but don't do anything with it.
PHP Code:
if ($file != '.' && $file != '..'
Change it to something like this
PHP Code:
if(in_array($sFile, array('.''..')) === true) {
continue; 
//skip the file
} else {
// Do something 
And the final thing is this.
PHP Code:
array_push($files,$file); 
According to php.net array_push it is better to use $array[] = $val if you add only one element to array
Quote:
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
Ultimatum is offline  
Reply With Quote
Old 04-09-2009, 11:33 AM   #11 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

we can condense it further with the SPL DirectoryIterator:
PHP Code:
$directory './';
$pDir = new DirectoryIterator($directory);
foreach(
$pDir as $fInf)
{
    if(
$fInf->isDot()) { continue; }
    if(
$fInf->isDir()) 
    { 
        echo 
'<strong>'$fInf->getFilename() , '</strong><br />'
    }
    else
    {
        echo 
$fInf->getFilename() , '<br />';
    }

__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 04-09-2009, 11:38 AM   #12 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
we can condense it further with the SPL DirectoryIterator:
PHP Code:
$directory './';
$pDir = new DirectoryIterator($directory);
foreach(
$pDir as $fInf)
{
    if(
$fInf->isDot()) { continue; }
    if(
$fInf->isDir()) 
    { 
        echo 
'<strong>'$fInf->getFilename() , '</strong><br />'
    }
    else
    {
        echo 
$fInf->getFilename() , '<br />';
    }

*Hint* The SPL docs at php.net could need a hand =)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 04-09-2009, 01:38 PM   #13 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Maybe I'm just misreading this thread but I think Peuplarchie is asking for the folders to be displayed first (in bold) and then the files afterwards.

Using the yummy SPL approach we can just iterate over the directory contents twice:
PHP Code:
$path  './path/to/dir';
$files = new DirectoryIterator($path);

// 1. List only folders
foreach ($files as $file)
{
    if (
$file->isDir() && ! $file->isDot())
        echo 
'<strong>'$file'</strong>'"\n";
}
// 2. List only files
foreach ($files as $file)
{
    if (
$file->isFile())
        echo 
$file"\n";

Salathe 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
How to search array benton General 3 03-29-2009 12:31 AM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
Build multi-dimensional array out of a flat array drewbee Advanced PHP Programming 2 05-28-2008 11:38 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 01:54 PM.

 
     

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