Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

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 10-21-2012, 08:53 PM   PM User | #1
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
Get last folder in directory array

This code will go into a directory on your website and get all the folders in that directory, and put them into an array.

PHP Code:
$folder = array(); // Creates a array for use later 

foreach (glob("*") as $thefolder) { // Use for each to go through and get each folder & file in the given directory 
    
If (is_dir($thefolder)) { // We only want to get folders so we are making sure that we are adding a directory and not a file. 
        
$folder[] = $thefolder// Adds the file to the array created 
    


$endfolder end($folder); //Last folder 
$endfolder now contains the Last folder in the directory.

Solution 2 (Credit to bjarneo):

PHP Code:
# Create a new DirectoryIterator object with the path you want to iterate through
$dir = new DirectoryIterator('/wamp/www/cf');

# Clean array
$cleanArr = array();

# Iterate through all directories, 
# and as long as they're not '.' or '..'(isDot), put them in array.
foreach($dir as $item) {
    if(
$item->isDir() && !$item->isDot()) {
        
$cleanArr[] = htmlentities($item->getPathname());
    }
}

$lastfolder end($cleanArr); 
$lastfolder now contains the last folder in the array.
markman641 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 04:42 AM.


Advertisement
Log in to turn off these ads.