Page 1 of 1

All Images from a folder.

Posted: Fri Feb 06, 2009 10:57 am
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

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 11:35 am
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?

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 11:42 am
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

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 11:52 am
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.

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 12:01 pm
by mickeyunderscore
Lol yea, PHP has a function to do almost anything, the difficulty is in remembering them all.

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 12:03 pm
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:

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 12:31 pm
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?

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 1:20 pm
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>';

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 2:00 pm
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.

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 2:31 pm
by mickeyunderscore
Try: $_SERVER["HTTP_HOST"];

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 2:40 pm
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"

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 2:42 pm
by mickeyunderscore
Well you have all the pieces, you've just got to stitch them together now.

Re: All Images from a folder.

Posted: Fri Feb 06, 2009 2:44 pm
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)