Jump to content

php code to list files in a dir


lixid

Recommended Posts

i was wondering if there was a dynamic way to list (ex. image.jpg ) images in a folder and then list them in a page as hyperlinks ... also would like to have thumbnail images in there and as long as they have a tn_ before the image name then it would pick that up as a thumbnail image and show the thumbnail. but in short words i would like to be able to just upload images into a folder and throw the script in there and have it be able to pick up the images in that folder and display them dynamicly .. im not asking for the code to do this .. but i am wondering how i would start ... thanx .. :]

Link to comment
https://forums.phpfreaks.com/topic/26211-php-code-to-list-files-in-a-dir/
Share on other sites

Im new to this myself but I'd start by making yourself a database in mySQL. As far as the thumbnails you would either have to use Photoshop to run a batch conversion on all the images you need OR get some sort of 3rd party script that makes thumbnails for you.
You can name it index.php and it will load the images or you can add it to existing code.
Here is a demo of the exact code below http://osphp.net/lixid/
[code]<?php
if ($handle = opendir('./images')) { // './filedir'
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "<p><a href='./images/$file'><img src='./images/$file' width='150' height='150' border='0' /></a><br /><a href='./images/$file'>$file</a></p>\n";
        }
    }
    closedir($handle);
}
?>[/code]

And here is a version that I added a little style to http://osphp.net/lixid/styled.php

Hope this helps :)
Hi, this link shows you how to list files in a dir and exclude directories.
you can download the whole php code at the bottom of this page.

[url=http://www.dev-tips.org/index.php?title=list_of_files]http://www.dev-tips.org/index.php?title=list_of_files[/url]

Here is a function that I have found and use, it gets all the images from a directory and passes
it back as an array. You can then loop through the array and make your link or what have you.

function getImagesList($path) {
    $ctr = 0;
    if ( $img_dir = @opendir($path) ) {
        while ( false !== ($img_file = readdir($img_dir)) ) {
            // can add checks for other image file types here
            if ( preg_match("/(\.gif|\.jpg)$/", $img_file) ) {
                $images[$ctr] = $img_file;
                $ctr++;
            }
        }
        closedir($img_dir);
        return $images;
    }
    return false;
}


cheers!
Stephen
just thought I might add a bit, you can also you .htaccess to list a directory.  The previous posts are probably best, just thought I'd throw this one out there.  http://www.clockwatchers.com/htaccess_dir.html (kinda ugly tutorial, but it works.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.