Page 1 of 1
PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:06 pm
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.
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:08 pm
by ta2shop
well you can try with adding 2 dots (../) before the folder name, you now have just one!
hope it helps.
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:10 pm
by jacek
Is it bad that it does that ?
You could remove the ./ form the paths as its not really needed.
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:11 pm
by nyo
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:12 pm
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
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:13 pm
by nyo
And I forgot to say that, I include gallery.php in resimler.php
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:15 pm
by jacek
I meant to remove the full "./"
If it works with it there though, what is the problem ?
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:24 pm
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
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:33 pm
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.
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 2:48 pm
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' />
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 3:20 pm
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.
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 3:42 pm
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
Nail
Re: PHP Tutorial: Automatic Image Gallery [part 01]
Posted: Mon May 09, 2011 4:11 pm
by jacek
no problem, glad its working