PHP Tutorial: Automatic Image Gallery [part 01]

Post here is you are having problems with any of the tutorials.
Post Reply
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

PHP Tutorial: Automatic Image Gallery [part 01]

Post by nyo »

Hi,

I am going over this tutorial at the moment and I am working locally. My gallery folder (images, gallery.php, thumbs) is not in the root directory, it is in images folder and my following code seems not to work:
<?php
if (is_dir('./images/gallery/thumbs') == false) {
	mkdir('./images/gallery/thumbs', 0744);
}
$dir = './images/gallery/';
$images = glob($dir.'*.{jpg,jpeg,png,gif}', GLOB_BRACE);

foreach ($images as $image) {
	if (file_exists("./images/gallery/thumbs/{$image}")) {
		echo "<a href=''><img src='images/gallery/thumbs/{$image}' alt='{$image}' /></a>";
	} else {
		echo "<a href=''><img src='?img={$image}' alt='{$image}' /></a>";
	}
}
?>
This script creates the thumbs folder, lists the filenames as an array successfully, but when I view one of the images as you do on 10:53 of the video, it shows this in the address bar:
http://localhost/resimler.php?img=./ima ... tel-03.jpg
I guess I have a mistake in the second echo, but I don't know what.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by ta2shop »

well you can try with adding 2 dots (../) before the folder name, you now have just one!
hope it helps.
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

Is it bad that it does that ?

You could remove the ./ form the paths as its not really needed.
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by nyo »

Two dots? But, this script works fine and echo also works but it gives
http://localhost/resimler.php?img=./ima ... tel-03.jpg
instead of something like
http://localhost/images/gallery/gallery ... tel-03.jpg
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by nyo »

jacek wrote:Is it bad that it does that ?

You could remove the ./ form the paths as its not really needed.
When I remove the dot it gives this error:

Warning: mkdir(): No such file or directory in C:\Apache2\htdocs\images\gallery\gallery.php on line 3
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by nyo »

And I forgot to say that, I include gallery.php in resimler.php
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

I meant to remove the full "./"

If it works with it there though, what is the problem ?
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by nyo »

jacek wrote:I meant to remove the full "./"

If it works with it there though, what is the problem ?
It doesn't list only the file names on the page like yours. It lists like that:

./images/gallery/melek-apart-hotel-01.jpg./images/gallery/melek-apart-hotel-02.jpg./images/gallery/melek-apart-hotel-03.jpg./images/gallery/melek-apart-hotel-04.jpg./images/gallery/melek-apart-hotel-05.jpg./images/gallery/melek-apart-hotel-06.jpg./images/gallery/melek-apart-hotel-07.jpg./images/gallery/melek-apart-hotel-08.jpg./images/gallery/melek-apart-hotel-09.jpg./images/gallery/melek-apart-hotel-10.jpg./images/gallery/melek-apart-hotel-11.jpg./images/gallery/melek-apart-hotel-12.jpg./images/gallery/melek-apart-hotel-13.jpg./images/gallery/melek-apart-hotel-14.jpg
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

Oh I SEE !

okay, its because you have added an extra directory level.

you will have to use scandir instead of glob I think. this will give you an array of file names which you can use.
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by nyo »

Ok, I moved the gallery folder to the root. But when I use "./thumbs" like you, the thumbs folder is created in the root, not in the gallery folder. Therefore, I am using "./gallery/thumbs" and it now creates the thumbs folder in the gallery folder as it should.

And this is the code I use now:
<?php

if (is_dir('./gallery/thumbs') == false) {
	mkdir('./gallery/thumbs', 0744);
}
$dir = './gallery/';
$images = glob($dir.'*.{jpg,jpeg,png,gif}', GLOB_BRACE);

foreach ($images as $image) {
	if (file_exists("./gallery/thumbs/{$image}")) {
		echo "<a href=''><img src='./gallery/thumbs/{$image}' alt='{$image}' /></a>";
	} else {
		echo "<a href=''><img src='?img={$image}' alt='{$image}' /></a>";
	}
}
?>
I think my echos has a problem, because when I view the source, I see this:

<a href=''><img src='?img=./gallery/melek-apart-hotel-01.jpg' alt='./gallery/melek-apart-hotel-01.jpg' />

Shouldn't I see this:

<a href=''><img src='?img=melek-apart-hotel-01.jpg' alt='melek-apart-hotel-01.jpg' />
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

In the video the script was in the same folder as the images, so your paths are wrong.

You may need to do something like this.
    <?php
     
    if (is_dir('./gallery/thumbs') == false) {
            mkdir('./gallery/thumbs', 0744);
    }

    $dir = './gallery/';
    $images = glob($dir.'*.{jpg,jpeg,png,gif}', GLOB_BRACE);
     
    foreach ($images as $image) {
            $image = basename($image);

            if (file_exists("./gallery/thumbs/{$image}")) {
                    echo "<a href=''><img src='./gallery/thumbs/{$image}' alt='{$image}' /></a>";
            } else {
                    echo "<a href=''><img src='?img={$image}' alt='{$image}' /></a>";
            }
    }
    ?>
The basename function will give you the file name, not the path.
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by nyo »

Hmm, my gallery.php script is in the same folder as the images, but I include gallery.php in resimler.php file where I want to show the image gallery.

That basename() function solved the issue, now it shows only image names.

Thank you very much Jacek. I hope you will not get bored from my questions because it seems more on the way :roll:

Nail
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Tutorial: Automatic Image Gallery [part 01]

Post by jacek »

no problem, glad its working :)
Image
Post Reply