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 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP Array Question

    Hey,

    I have an array of all the folders in a directory using scandir and i then want to echo out the array but not using print_r. I just want a list of all the folders but the array is going to change depending on the number of folders and i want to be able to echo them all out except the last 2 which are just . and ..

    My code is below -

    PHP Code:
    $dir    'templates';
    $files scandir($dir1);

    echo 
    "<pre>";
    print_r($files);
    echo 
    "</pre>"

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    $files = scandir($dir, 1);
    Use
    PHP Code:
    $files=glob($dir."/*"); 
    instead.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks, How can i echo them out outwith the array like

    test
    default

    instead of

    Array (
    [0] => test
    [1] => default
    )

    But all the array and not specific ones

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    PHP Code:
    foreach($files as $f){
     echo 
    $f.'<br />';
    }

    #or
    echo implode('<br />'$files); 
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    $files scandir($folder);
    foreach(
    $files as $file) {
        if ((
    $file != '.') && ($file != '..')) {
            echo(
    $file."\n");
        }

    To display folders only:

    PHP Code:
    $files scandir($folder);
    foreach(
    $files as $file) {
        if ((
    $file != '.') && ($file != '..') && is_dir($folder.'/'.$file)) {
            echo(
    $file."\n");
        }

    To display files only:

    PHP Code:
    $files scandir($folder);
    foreach(
    $files as $file) {
        if (
    is_file($folder.'/'.$file)) {
            echo(
    $file."\n");
        }

    Untested.
    Last edited by Lamped; 12-01-2010 at 11:59 AM. Reason: Added folder and file only code
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    echo implode('<br>',$files); 
    Edit: Too late
    Last edited by abduraooft; 12-01-2010 at 11:59 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    New Coder
    Join Date
    Oct 2010
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for your help


  •  

    Posting Permissions

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