All Images from a folder.
Moderator: General Moderators
- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
All Images from a folder.
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
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
- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
Re: All Images from a folder.
I've tried this
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?
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
}
}
?>
-
mickeyunderscore
- Forum Contributor
- Posts: 129
- Joined: Sat Jan 31, 2009 9:00 am
- Location: UK
Re: All Images from a folder.
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
- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
Re: All Images from a folder.
Hey! That worked amazingly!!! super simple.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
This does exactly what I need.
Code: Select all
foreach (glob("*.jpg") as $filename) {
echo "$filename\n";
}
-
mickeyunderscore
- Forum Contributor
- Posts: 129
- Joined: Sat Jan 31, 2009 9:00 am
- Location: UK
Re: All Images from a folder.
Lol yea, PHP has a function to do almost anything, the difficulty is in remembering them all.
- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
Re: All Images from a folder.
Lol. For me it's trying to figure out how to use what the manual gives me.mickeyunderscore wrote:Lol yea, PHP has a function to do almost anything, the difficulty is in remembering them all.
- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
Re: All Images from a folder.
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?
Any ideas?
-
mickeyunderscore
- Forum Contributor
- Posts: 129
- Joined: Sat Jan 31, 2009 9:00 am
- Location: UK
Re: All Images from a folder.
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>';- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
Re: All Images from a folder.
I did that and all it gave me was the blablahfilename.jpg.
Array ( [0] => phpjunkie.jpg [1] => texan2.jpg )
Thanks for the help.
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.
Try: $_SERVER["HTTP_HOST"];
- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
Re: All Images from a folder.
Hey, I really do appreciate your help. Here's what i'm looking for.mickeyunderscore wrote:Try: $_SERVER["HTTP_HOST"];
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.
Well you have all the pieces, you've just got to stitch them together now.
- Skoalbasher
- Forum Contributor
- Posts: 147
- Joined: Thu Feb 07, 2008 8:09 pm
Re: All Images from a folder.
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)mickeyunderscore wrote:Well you have all the pieces, you've just got to stitch them together now.