Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post

    Get last folder in directory

    SOLUTION FOUND, LOOK DAT BOTTOM OF POST

    Okay, i've gone through many attempts and I can't seen to get this to work.
    What I'm trying to do is go into a folder and get the LAST folder in there. Then increment that value by one. I can't get it to work. Here is my code.

    PHP Code:
    $path '../campaigns/';
    $results scandir($path);

    foreach (
    $results as $result) {
        if (
    $result === '.' or $result === '..') continue;

        if (
    is_dir($path '/' $result)) {
            
    //code to use if directory
        
    }

    I way to do this is somehow put all the results into one string, then get the last character of the string.. how can that be done?


    EDIT: Found an easier solution!
    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);
    $endfolder++;
    echo 
    $endfolder
    Last edited by markman641; 10-21-2012 at 02:38 AM.

  • #2
    New Coder
    Join Date
    May 2012
    Posts
    50
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Here's another solution:
    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());
        }
    }

    echo 
    end($cleanArr); 
    Note: this will only get the last dir.
    Last edited by bjarneo; 10-21-2012 at 08:28 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •