Welcome to this short tutorial on how to list images in a directory.
This tutorial deals with listing files and manipulating them to use an html code, like an image tag, to echo out the images that are listed in a directory.
The end result is going to look something like this.
I've created 8 images and they are just going to be listed down a page. These are all separate images here.
Let me just show you how I've set up my directory structure - its like this.
I've got my 'show dot php' file which is the one we're going to work with here.
And then I have my images folder and that's got the images which are listed here, as shown.
It doesn't matter what format the are in, at all.
They could be mixed format, they could be one format and any format of display or image file via html will work.
So here's our 'show dot php'.
And at the moment this is pretty much blank inside here.
And obviously we'll need our php tags.
And the way we'll be doing this is we'll first of all set up a variable with the directory of our images.
And as I showed you earlier this is 'images' and we'll just have a forward slash in there.
Be careful with these symbols like back-slashes. They are special characters in php that get rid of the character after it.
So for example if you have 'images forward slash photos' then this would be writing php as 'images-hotos' because this character here cancels the 'p'.
So make sure your using forward slash and obviously we don't have this 'photos' here.
Okay the next thing we need to do is use the 'open dir function'.
This is basically going to open a directory for us.
It's not going to be the contents of the directory.
This is just going to open a specific directory namely this directory here.
So instead of keeping this as it is what we'll do is say if open dir equals to a new variable called 'open dir' and dir. So we're matching this to this here.
And basically all this does is, it says whether this has successfully completed and then it assigns 'open dir' to our open directory so we can manipulate it later on.
The reason that we do this is because if your directory doesn't exist, we'll get a bunch of code and lots of errors.
This here says if we don't have any errors then we can continue with the code inside and our block here.
Okay now the next bit is more complicated.
Let's just start annotating this.
This is to open the directory.
And inside what we'll be doing is we'll be reading the directory so 'read dir'.
And we'll do this with a while loop because for each loop around the while loop we want to echo out or rather display the image of each image inside the folder.
So we'll start with our while loop, we'll create what will go in it, in a minute and here's our block of code we'll execute, in between here, for our while loop.
Okay for this what we'll do is if file equals read directory, that's a new function I've just introduced.
And obviously you may have guessed you have to type 'open dir' variable inside here.
So this is basically reading the directory that we've already opened using the open dir function.
So these are two really useful functions so I can harmonize them with each other.
And again we'll validate this here and say if it doesn't equal 'false' or if it's not equal to or if it doesn't open or if it can't be read that means we may run into a few errors later on.
And with this we need to start the structure.
We'll need to put this into parenthesis.
So lets just put that into parenthesis.
Okay so that should be our complete while statement.
Now inside there's a really easy way to do this because we've created this file variable.
And we're inside a while loop so this is gonna dynamically update for each file contained within this directory.
So all we really need to do is now say 'echo file' and what we might want to do is add a 'br'to the end of that.
So if we open up our browser and refresh, you can see that we've got all our directories listed.
Now I do have some other tutorials on directory listing I think I've mentioned this before.
We've got a dot and a double dot,this is basically just standard notations for directory structures.
dot i think is current directory, two dots is to go back or something like that.
but what we need to do now is validate this inside our loop to make sure we don't echo out this dot and this full stop - these two dots, sorry.
Reason is that if we are displaying these as images that is not a valid image and that is not a valid image.
So we'll need to get rid of these.
So what I'll do is I'll say if file doesn't equal dot and - we need an 'and' here instead of an 'or' - and file doesn't equal dot dot.
So as we're looping through its going to say "Does this equal dot?"
In the first case its going to be 'yes' so we'll completely ignore this statement inside our if - the command inside our if statement.
And we're also checking for this simultaneously, so obviously both are going to be true.
So now what we'll do is refresh and we see that they have disappeared.
Okay so the next thing to do is to manipulate this file variable here to actually make an image.
So what we'll do is I'll take away all of this and I'll just write some html code as an omni word.
So image source equals something in here.
You can specify a height and a width but I'll not do this now because my image is all preset height and width.
Obviously if you have images of all different sizes you will want to put them all in the same size and then maybe have a hyper link to them so you can click them to view them individually.
But that's pretty straight forward and I'll just show you the php code for doing it.
Okay and then we'll have a break after each one.
So inside here, funnily enough, you might think it's going to put 'file' but instead when you refresh this, you can see we've got a bunch of broken images.
The reason is that if I click properties you can see here we've said directory images and image 1.
We need our images directory in here.
So we could write images but we already have a variable for that, that's 'dir'.
So we just say 'dir forward slash file' so this will be images forward slash file.
So now when we refresh you will see that we're basically back to the page I showed you at the start of this tutorial.
So that's basically it. There are more advanced ways of doing things, to lay them out, etc.
But if you are having any trouble then please contact me. I'll be glad to help
All right then thanks a lot for watching. This is Joshua Mathew dubbing for the Spoken Tutorial Project.