All Images from a folder.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

All Images from a folder.

Post by Skoalbasher »

Hey there,

I've been looking around on google and here and can't find exactly what I need. i think this should be a pretty easy task. I have an images folder where I store these neat little signature graphics that I have. Instead of going to the /images/ folder and just seeing a directory, I'd like to have an index.php file.

I can make a loop to echo each file, and number them 1-whatever. But then i'd have to change the PHP code everytime I uploaded a file. I don't want someone to give me the answer, but more of a suggestion of where to start.

If possible I'd like it to pull just whatever file was in there regardles of the name, print it out and then do a <br> or something. I know how to do all that, just don't know how to pull all files froma directory regardless of their name.

Thanks
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: All Images from a folder.

Post by Skoalbasher »

I've tried this

Code: Select all

 
<?php
$dirname = "../storage/";
    
$dir = opendir($dirname);
    
while(false != ($file = readdir($dir)))
    {
    if(($file != ".") and ($file != ".."))
        {
            $filearray = explode(".", $file);  // here
            if($filearray[1] == "jpg"); // here
            {  // here
            echo($file)."<br>";
            } // here
        }
    }
?>
 
I've gotten some code off the net. I put in the commented parts to check for the .jpg. Just displays everything in the folder. As a list. Any ideas of how to extract just the jpg files?
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: All Images from a folder.

Post by mickeyunderscore »

You may find it easier to use the glob() function as it returns an array of file information which is easy to work with: http://uk3.php.net/glob
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: All Images from a folder.

Post by Skoalbasher »

mickeyunderscore wrote:You may find it easier to use the glob() function as it returns an array of file information which is easy to work with: http://uk3.php.net/glob
Hey! That worked amazingly!!! super simple.

This does exactly what I need.

Code: Select all

 
foreach (glob("*.jpg") as $filename) {
    echo "$filename\n";
}
 
Thanks! I didn't even have to declare a directory or anything.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: All Images from a folder.

Post by mickeyunderscore »

Lol yea, PHP has a function to do almost anything, the difficulty is in remembering them all.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: All Images from a folder.

Post by Skoalbasher »

mickeyunderscore wrote:Lol yea, PHP has a function to do almost anything, the difficulty is in remembering them all.
Lol. For me it's trying to figure out how to use what the manual gives me. :banghead:
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: All Images from a folder.

Post by Skoalbasher »

Ok, now I need to know how to get the path. I mean, i can get the /path by using $tester = $_SERVER['PHP_SELF']; but I want the "http://www.blah.com" of whatever page i have this php in.

Any ideas?
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: All Images from a folder.

Post by mickeyunderscore »

glob() returns an array containing paths, inspect the array it returns to see:

Code: Select all

$files = glob("*.jpg");
echo '<pre>';
print_r($files);
echo '</pre>';
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: All Images from a folder.

Post by Skoalbasher »

I did that and all it gave me was the blablahfilename.jpg.

Array ( [0] => phpjunkie.jpg [1] => texan2.jpg )

Thanks for the help.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: All Images from a folder.

Post by mickeyunderscore »

Try: $_SERVER["HTTP_HOST"];
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: All Images from a folder.

Post by Skoalbasher »

mickeyunderscore wrote:Try: $_SERVER["HTTP_HOST"];
Hey, I really do appreciate your help. Here's what i'm looking for.

i have images at

http://www.schoolofdan.com/storage/imagename.jpg

i want the "www.schoolofdan.com/storage"

If i get the path I get /storage/index.php.

If i do the server host i get "www.schoolofdan.com"
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: All Images from a folder.

Post by mickeyunderscore »

Well you have all the pieces, you've just got to stitch them together now.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: All Images from a folder.

Post by Skoalbasher »

mickeyunderscore wrote:Well you have all the pieces, you've just got to stitch them together now.
Lol, yeah that's what I was trying to avoid. Oh well. Thanks for your time Mickey. I'm happy with my results, was just looking for something a little faster/cleaner. (not that it's slow)
Post Reply