Results 1 to 4 of 4

Thread: I need to improve on this code that displays all files in a folder

  1. #1
    Join Date
    Apr 2008
    Posts
    84
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default I need to improve on this code that displays all files in a folder

    Hi, all.
    I currently have this PHP code on my site to display all the videos I have in one folder (so I don't have to update the HTML every time I add or delete one):

    Code:
    <?
    
        $path = "/home/content/d/e/f/defensecenter/html/videos/student002";
    
        $dir_handle = @opendir($path) or die("Unable to open $path");
    
        while ($file = readdir($dir_handle)) {
    
        if($file == "." || $file == ".." || $file == "index.php" )
    
            continue;
            echo "<div class='vidlink'><a href=\"$file\">$file</a></div>";
    
        }
    
        closedir($dir_handle);
    ?>
    But it has 2 problems:
    1. It displays the .htaccess file which I have there for password protection (not a big deal, it opens to an error page, but I'd prefer it not show at all).

    2. It doesn't put them in alphanumerical order, it seams to just spit them up randomly (though the files are in the same order every time I visit/refresh the page).

    Any help would be appreciated. I know little to nothing about PHP, I got that code off the web.

    I could also use a good video tutorial on beginning PHP. I tried Lynda.com and a couple others, but found them very confusing, and I'm not dense, I do OK with HTML and CSS, but I can't get my head around PHP from any tutorial I've found.

    Thanks a lot!
    -Geezer

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    PHP Code:
        $path '/home/content/d/e/f/defensecenter/html/videos/student002';
        
    $dir_handle = @scandir($path);
        if(
    $dir_handle){
            foreach(
    $dir_handle as $file){
                if(!
    in_array($file, array('..''.''.htaccess''index.php'))){
                    echo 
    "<div class='vidlink'><a href=\"$file\">$file</a></div>";
                }
            }
        } 

  3. #3
    Join Date
    Apr 2008
    Posts
    84
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Worked like a charm, thanks a million.

    Any suggestions on video tutorials for PHP would also be appreciated.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PHP 101 is easy to follow, humorous, and engaging. That said, however, there are typos in some lines of script, and pieces from earlier lessons (or prerequisite html code) are sometimes omitted, so I would only recommend it if you're okay with problem solving. I actually ended up learning quite a bit from the mistakes. The other issue is that it was written quite a while ago, so while it covers PHP5, most stuff is PHP4-oriented.

    Tzag is a good tutorial also. It covers more aspects of PHP in a more typical "tutorial" manner, meaning it was written "for beginners" by people who are definitely not beginners and can't quite think like beginners. I've heard PHP guys offer complaints about missing/inaccurate stuff in these tutorials, but I haven't run across any problems myself.

    Edit: sorry, I didn't see that you wanted video tutorials.
    Last edited by traq; 06-05-2009 at 12:59 AM.

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
  •