Results 1 to 9 of 9

Thread: Display Directory Contents Without Extentions Included

  1. #1
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Display Directory Contents Without Extentions Included

    Is there a way to display all the file in a directory but not have the file extentions included? For Example:

    Directory Contents

    file
    file2


    NOT



    Directory Contents

    file.php
    file2.html



    Thanks!

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Yup, http://php.net/manual/en/function.readdir.php

    If you will only ever have one period in your file name this should (untested) work, actually this may work if there is more than 1 period as well.
    PHP Code:
    <?php
    $directory 
    '/path/to/files';
    if (
    $handle opendir($directory)) {
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && $file != "..") {
    $file preg_replace('/(.*)\..*/''$1'$file);
    echo 
    "$file\n";
            }
        }
        
    closedir($handle);
    }
    ?>
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    Hmm. What I'm using to test my site is local host and when I put your script into my page, it returns with: All Users Default Default User desktop Mark Public TEMP

    What happened and what did I do wrong (if anything) and how to fix it.

    Thanks!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    A bit of a novice at PHP, I came up with this:

    PHP Code:
    <?php
    function listnoext(){
        if(
    $handle opendir('.')){
            
    $pattern '#\.[^\.]+$#';
            
    clearstatcache();
            while(
    false !== ($file readdir($handle))){
                
    $files[] = preg_replace($pattern''$file);
            }
            
    closedir($handle);
        }
        echo 
    implode('<br>'$files);
    }
    echo 
    'Directory Contents:<br>';
    listnoext();
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    It sounds like you outputted the wrong directory. Were the files you want in any of those folders?

    All Users Default Default User desktop Mark Public TEMP
    Corrections to my coding/thoughts welcome.

  6. #6
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    Fixed the issue! Thank you so much for your help bluewalrus and jscheuer1!

  7. #7
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default taking it a bit further

    Quote Originally Posted by jscheuer1 View Post
    A bit of a novice at PHP, I came up with this:

    PHP Code:
    <?php
    function listnoext(){
        if(
    $handle opendir('.')){
            
    $pattern '#\.[^\.]+$#';
            
    clearstatcache();
            while(
    false !== ($file readdir($handle))){
                
    $files[] = preg_replace($pattern''$file);
            }
            
    closedir($handle);
        }
        echo 
    implode('<br>'$files);
    }
    echo 
    'Directory Contents:<br>';
    listnoext();
    ?>
    This works the best so far but I was wondering if the list could somehow show the directories in a way they can easily be seen( as to making them easily distingished from other file types.)

    _pardon my spelling please.

    bud

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    How about:

    PHP Code:
    <?php
    function listnoext(){
        if(
    $handle opendir('.')){
            
    $pattern '#\.[^\.]+$#';
            
    clearstatcache();
            while(
    false !== ($file readdir($handle))){
                if(
    is_dir($file)){
                    
    $files[] = '<span style="color: red;">' preg_replace($pattern''$file) . '</span>';
                } else {
                    
    $files[] = preg_replace($pattern''$file);
                }
            }
            
    closedir($handle);
        }
        echo 
    implode('<br>'$files);
    }
    echo 
    'Directory Contents:<br>';
    listnoext();
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. The Following User Says Thank You to jscheuer1 For This Useful Post:

    ajfmrf (08-16-2011)

  10. #9
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default well done John

    That works just fine John.

    Thanks for the quick fix


    Bud

Bookmarks

Posting Permissions

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