Automated PHP gallery

I’m fairly new to web design and programming, but I’ve found my way around until now.
I have one question and i hope that someone will be able to answer it, because I’ve spent a few weeks trying to find the answer with no luck at all.
I’m running the website for my parents’ job and I was trying to create an automated gallery. The thing is, I made a gallery, but it’s a lot of work to code the images into it, so I wondered if there is a way to make a script that automatically implements the pictures I FTP to the server into their respective folders
I’ve seen a lot of automated galleries, but I had no luck implementing it so that it “writes” this code for each image:

<a href=“…/slike/Dječje/Majice/Majica ivan š-340 vel. 1-6.jpg”>
<img title=“Majica ivan”
<img src=“…/slike/Dječje/Majice/Majica ivan š-340 vel. 1-6.jpg” width=“500” height=“333” />

So, the thing is that i need the script to send the path of each image in a certain directory and put it to <a href=“”> and <img src=“”> , and take the name of the image and put it into <img title=“”>.

Thanks in advance!

Are you using a database to store the image names?

No, but if needed I can start using it.

You can use glob and loop through directories and display all images. Also foreach will also work.

Well, if it’s not a problem, could someone please write the code. I get the idea of how it could work, but I had no luck in writing a script that does that. Thank you.

The best way to use a forum like this is to show what you tried, and then people can talk you through the changes to make. It doesn’t matter how poor the attempt might be, as log as you make a start. (It’s fine if you’d rather just have someone do it for you, but that’s when you need to look at hiring someone. :slight_smile: But that isn’t what the forum is for.)

I completely agree with you, but I’m not interested in just getting the script, but I want to understand how it works, since I’m just 17 and plan to be a web developer for living. Since I’m fairly new to php, using it only for including menus, footers and other repetitive parts of my website, I’m not used to it’s code. I’ve spent hours reading through tutorials about php, but the problem is that i know what the script has to do, but have no idea how to make something do it. I suppose that I need it to get the number of files in a certain directory, then make it go from 1 to the number of files, doing the same for each of them, and that is getting their name and path. After getting that, it needs to implement the paths into the source of the img (the image that it presents) and the href of the thumbnail. So, instead of asking someone the write the code down for me, can someone tell me what functions to use for that, so I can make my own code and than have someone help me correct it? Thank you.

I suppose i could use basename for getting the name of the image, realpath for the path of the image, and something like this for the number of images in the folder:
$directory = “/images/subdir1”;
if (glob($directory . “.jpg") != false)
{
$filecount = count(glob($directory . "
.jpg”));
echo $filecount;
}
else
{
echo 0;

<?php
$directory = “/images”;
$file**** =count(glob($directory . “.jpg”));
global $filecount
$i=0;
function gallery()
{
$i++
while (i<=$filecount) {
echo '<a href=How to make it write the path of each file?>
<img title=“How to make it write the name of each file?
<img src=How to make it write the path of each file? width=“500” height=“333” />

}
}
?>

It can be a bit bewildering can’t it? I don’t want to sound harsh but your example has some basic HTML syntax errors so perhaps if you strengthen your HTML skills you will have less problems writing PHP.

Anyway, I always find the documentation a big help. For [fphp]glob()[/fphp] the example is

<?php
foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\
";
}
?> 

then picking apart your code in a guess at what you’re trying to do, I substituted code into the example and added a debug section of code

<?php
$directory = "/images";

/* for testing only */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
$test = glob($directory . "*.jpg");
var_dump($test);

foreach (glob($directory . "*.jpg") as $filename) {
		echo '<a href="' . $directory . $filename . '">
			<img src="' . $directory . $filename . '" width="500" height="333" title="' . $filename . ' /></a>';
}
?> 

I see the mistakes, and those were made because I was in a hurry, but I completely agree with you that i need to work on my html skills.
The problem I have now is that when i specify the directory as “…/slike/Dječje/Majice/” it returns the message that there are 0 files in the array. The path of the folder with the images is root/slike/dječje/majice/ and the path to the website is root/galerija/dječje-majice.php. html has no problem with the path “…/slike/Dječje/Majice/” , but when i try to use it in php it doesn’t recognize it. Is the problem maybe the special character č ?
And when i put the website with the php file in the folder root/slike/dječje and set the directory to “majice/” it recognizes it, but it writes this:
array(32) {
[0]=>
string(37) “Majice/Majica ivan �-340 vel. 1-6.jpg”
}
<a href=“Majice/Majice/Majica ivan �-340 vel. 1-6.jpg”>
<img src=“Majice/Majice/Majica ivan �-340 vel. 1-6.jpg” width=“500” height=“333” title="Majice/Majica ivan �-340 vel. 1-6.jpg /></a><a href=“Majice/Majice/Majica ivan �-341 vel. 8-12 (2).jpg”

instead of this

        &lt;a href="../slike/Dje&#269;je/Majice/Majica ivan &#353;-340 vel. 1-6.jpg"&gt;
        	     &lt;img src="../slike/Dje&#269;je/Majice/Majica ivan &#353;-340 vel. 1-6.jpg" width="500" height="333" title="Majica ivan &#353;-340 vel. 1-6" /&gt;

for every image respectively and i have absolutely no idea why it does that.
Thank you.

first sorting out

[0]=>
string(37) “Majice/Majica ivan �-340 vel. 1-6.jpg”

for every image and after that

<a href=“Majice/Majice/Majica ivan �-340 vel. 1-6.jpg”>
<img src=“Majice/Majice/Majica ivan �-340 vel. 1-6.jpg” width=“500” height=“333” title="Majice/Majica ivan �-340 vel. 1-6.jpg /></a><a href=“Majice/Majice/Majica ivan �-341 vel. 8-12 (2).jpg”

Making it look like this, and doing absolutely nothing

array(32) {
[0]=>
string(37) “Majice/Majica ivan �-340 vel. 1-6.jpg”
[1]=>
string(42) “Majice/Majica ivan �-341 vel. 8-12 (2).jpg”
[2]=>
string(38) “Majice/Majica ivan �-347 vel. 8-12.jpg”
[3]=>
string(40) “Majice/Majica ivan(3) �-346 vel. 1-6.jpg”
[4]=>
string(40) “Majice/Majica mako(3) �-331 vel. 2-6.jpg”
[5]=>
string(41) “Majice/Majica mako(3) �-332 vel. 8-12.jpg”
[6]=>
string(42) “Majice/Majica mako(3) �-333 vel. 14-16.jpg”
[7]=>
string(44) “Majice/Majica uska k.r. �-300 vel. 14-16.jpg”
[8]=>
string(42) “Majice/Majica uska k.r. �-311 vel. 2-6.jpg”
[9]=>
string(43) “Majice/Majica uska k.r. �-312 vel. 8-12.jpg”
[10]=>
string(37) “Majice/Majica uska �-359 vel. 2-6.jpg”
[11]=>
string(38) “Majice/Majica uska �-360 vel. 8-12.jpg”
[12]=>
string(42) “Majice/Majica uska(1) �-324 vel. 14-16.jpg”
[13]=>
string(40) “Majice/Majica uska(2) �-325 vel. 2-6.jpg”
[14]=>
string(41) “Majice/Majica uska(2) �-326 vel. 8-12.jpg”
[15]=>
string(42) “Majice/Majica uska(2) �-327 vel. 14-16.jpg”
[16]=>
string(46) “Majice/Majica uska(D) �-363 vel. 14-16 (2).jpg”
[17]=>
string(46) “Majice/Majica uska(D) �-363 vel. 14-16 (3).jpg”
[18]=>
string(42) “Majice/Majica uska(D) �-363 vel. 14-16.jpg”
[19]=>
string(36) “Majice/Majica �-322 vel. 2-6 (2).jpg”
[20]=>
string(36) “Majice/Majica �-322 vel. 2-6 (3).jpg”
[21]=>
string(36) “Majice/Majica �-322 vel. 2-6 (4).jpg”
[22]=>
string(36) “Majice/Majica �-322 vel. 2-6 (5).jpg”
[23]=>
string(32) “Majice/Majica �-322 vel. 2-6.jpg”
[24]=>
string(37) “Majice/Majica �-323 vel. 8-12 (2).jpg”
[25]=>
string(37) “Majice/Majica �-323 vel. 8-12 (3).jpg”
[26]=>
string(37) “Majice/Majica �-323 vel. 8-12 (4).jpg”
[27]=>
string(33) “Majice/Majica �-323 vel. 8-12.jpg”
[28]=>
string(38) “Majice/Majica �-324 vel. 14-16 (2).jpg”
[29]=>
string(38) “Majice/Majica �-324 vel. 14-16 (3).jpg”
[30]=>
string(38) “Majice/Majica �-324 vel. 14-16 (4).jpg”
[31]=>
string(34) “Majice/Majica �-324 vel. 14-16.jpg”
}
<a href=“Majice/Majice/Majica ivan �-340 vel. 1-6.jpg”>
<img src=“Majice/Majice/Majica ivan �-340 vel. 1-6.jpg” width=“500” height=“333” title="Majice/Majica ivan �-340 vel. 1-6.jpg /></a><a href=“Majice/Majice/Majica ivan �-341 vel. 8-12 (2).jpg”>
<img src=“Majice/Majice/Majica ivan �-341 vel. 8-12 (2).jpg” width=“500” height=“333” title="Majice/Majica ivan �-341 vel. 8-12 (2).jpg /></a><a href=“Majice/Majice/Majica ivan �-347 vel. 8-12.jpg”>
<img src=“Majice/Majice/Majica ivan �-347 vel. 8-12.jpg” width=“500” height=“333” title="Majice/Majica ivan �-347 vel. 8-12.jpg /></a><a href=“Majice/Majice/Majica ivan(3) �-346 vel. 1-6.jpg”>
<img src=“Majice/Majice/Majica ivan(3) �-346 vel. 1-6.jpg” width=“500” height=“333” title="Majice/Majica ivan(3) �-346 vel. 1-6.jpg /></a><a href=“Majice/Majice/Majica mako(3) �-331 vel. 2-6.jpg”>
<img src=“Majice/Majice/Majica mako(3) �-331 vel. 2-6.jpg” width=“500” height=“333” title="Majice/Majica mako(3) �-331 vel. 2-6.jpg /></a><a href=“Majice/Majice/Majica mako(3) �-332 vel. 8-12.jpg”>
<img src=“Majice/Majice/Majica mako(3) �-332 vel. 8-12.jpg” width=“500” height=“333” title="Majice/Majica mako(3) �-332 vel. 8-12.jpg /></a><a href=“Majice/Majice/Majica mako(3) �-333 vel. 14-16.jpg”>
<img src=“Majice/Majice/Majica mako(3) �-333 vel. 14-16.jpg” width=“500” height=“333” title="Majice/Majica mako(3) �-333 vel. 14-16.jpg /></a><a href=“Majice/Majice/Majica uska k.r. �-300 vel. 14-16.jpg”>
<img src=“Majice/Majice/Majica uska k.r. �-300 vel. 14-16.jpg” width=“500” height=“333” title="Majice/Majica uska k.r. �-300 vel. 14-16.jpg /></a><a href=“Majice/Majice/Majica uska k.r. �-311 vel. 2-6.jpg”>
<img src=“Majice/Majice/Majica uska k.r. �-311 vel. 2-6.jpg” width=“500” height=“333” title="Majice/Majica uska k.r. �-311 vel. 2-6.jpg /></a><a href=“Majice/Majice/Majica uska k.r. �-312 vel. 8-12.jpg”>
<img src=“Majice/Majice/Majica uska k.r. �-312 vel. 8-12.jpg” width=“500” height=“333” title="Majice/Majica uska k.r. �-312 vel. 8-12.jpg /></a><a href=“Majice/Majice/Majica uska �-359 vel. 2-6.jpg”>
<img src=“Majice/Majice/Majica uska �-359 vel. 2-6.jpg” width=“500” height=“333” title="Majice/Majica uska �-359 vel. 2-6.jpg /></a><a href=“Majice/Majice/Majica uska �-360 vel. 8-12.jpg”>
<img src=“Majice/Majice/Majica uska �-360 vel. 8-12.jpg” width=“500” height=“333” title="Majice/Majica uska �-360 vel. 8-12.jpg /></a><a href=“Majice/Majice/Majica uska(1) �-324 vel. 14-16.jpg”>
<img src=“Majice/Majice/Majica uska(1) �-324 vel. 14-16.jpg” width=“500” height=“333” title="Majice/Majica uska(1) �-324 vel. 14-16.jpg /></a><a href=“Majice/Majice/Majica uska(2) �-325 vel. 2-6.jpg”>
<img src=“Majice/Majice/Majica uska(2) �-325 vel. 2-6.jpg” width=“500” height=“333” title="Majice/Majica uska(2) �-325 vel. 2-6.jpg /></a><a href=“Majice/Majice/Majica uska(2) �-326 vel. 8-12.jpg”>
<img src=“Majice/Majice/Majica uska(2) �-326 vel. 8-12.jpg” width=“500” height=“333” title="Majice/Majica uska(2) �-326 vel. 8-12.jpg /></a><a href=“Majice/Majice/Majica uska(2) �-327 vel. 14-16.jpg”>
<img src=“Majice/Majice/Majica uska(2) �-327 vel. 14-16.jpg” width=“500” height=“333” title="Majice/Majica uska(2) �-327 vel. 14-16.jpg /></a><a href=“Majice/Majice/Majica uska(D) �-363 vel. 14-16 (2).jpg”>
<img src=“Majice/Majice/Majica uska(D) �-363 vel. 14-16 (2).jpg” width=“500” height=“333” title="Majice/Majica uska(D) �-363 vel. 14-16 (2).jpg /></a><a href=“Majice/Majice/Majica uska(D) �-363 vel. 14-16 (3).jpg”>
<img src=“Majice/Majice/Majica uska(D) �-363 vel. 14-16 (3).jpg” width=“500” height=“333” title="Majice/Majica uska(D) �-363 vel. 14-16 (3).jpg /></a><a href=“Majice/Majice/Majica uska(D) �-363 vel. 14-16.jpg”>
<img src=“Majice/Majice/Majica uska(D) �-363 vel. 14-16.jpg” width=“500” height=“333” title="Majice/Majica uska(D) �-363 vel. 14-16.jpg /></a><a href=“Majice/Majice/Majica �-322 vel. 2-6 (2).jpg”>
<img src=“Majice/Majice/Majica �-322 vel. 2-6 (2).jpg” width=“500” height=“333” title="Majice/Majica �-322 vel. 2-6 (2).jpg /></a><a href=“Majice/Majice/Majica �-322 vel. 2-6 (3).jpg”>
<img src=“Majice/Majice/Majica �-322 vel. 2-6 (3).jpg” width=“500” height=“333” title="Majice/Majica �-322 vel. 2-6 (3).jpg /></a><a href=“Majice/Majice/Majica �-322 vel. 2-6 (4).jpg”>
<img src=“Majice/Majice/Majica �-322 vel. 2-6 (4).jpg” width=“500” height=“333” title="Majice/Majica �-322 vel. 2-6 (4).jpg /></a><a href=“Majice/Majice/Majica �-322 vel. 2-6 (5).jpg”>
<img src=“Majice/Majice/Majica �-322 vel. 2-6 (5).jpg” width=“500” height=“333” title="Majice/Majica �-322 vel. 2-6 (5).jpg /></a><a href=“Majice/Majice/Majica �-322 vel. 2-6.jpg”>
<img src=“Majice/Majice/Majica �-322 vel. 2-6.jpg” width=“500” height=“333” title="Majice/Majica �-322 vel. 2-6.jpg /></a><a href=“Majice/Majice/Majica �-323 vel. 8-12 (2).jpg”>
<img src=“Majice/Majice/Majica �-323 vel. 8-12 (2).jpg” width=“500” height=“333” title="Majice/Majica �-323 vel. 8-12 (2).jpg /></a><a href=“Majice/Majice/Majica �-323 vel. 8-12 (3).jpg”>
<img src=“Majice/Majice/Majica �-323 vel. 8-12 (3).jpg” width=“500” height=“333” title="Majice/Majica �-323 vel. 8-12 (3).jpg /></a><a href=“Majice/Majice/Majica �-323 vel. 8-12 (4).jpg”>
<img src=“Majice/Majice/Majica �-323 vel. 8-12 (4).jpg” width=“500” height=“333” title="Majice/Majica �-323 vel. 8-12 (4).jpg /></a><a href=“Majice/Majice/Majica �-323 vel. 8-12.jpg”>
<img src=“Majice/Majice/Majica �-323 vel. 8-12.jpg” width=“500” height=“333” title="Majice/Majica �-323 vel. 8-12.jpg /></a><a href=“Majice/Majice/Majica �-324 vel. 14-16 (2).jpg”>
<img src=“Majice/Majice/Majica �-324 vel. 14-16 (2).jpg” width=“500” height=“333” title="Majice/Majica �-324 vel. 14-16 (2).jpg /></a><a href=“Majice/Majice/Majica �-324 vel. 14-16 (3).jpg”>
<img src=“Majice/Majice/Majica �-324 vel. 14-16 (3).jpg” width=“500” height=“333” title="Majice/Majica �-324 vel. 14-16 (3).jpg /></a><a href=“Majice/Majice/Majica �-324 vel. 14-16 (4).jpg”>
<img src=“Majice/Majice/Majica �-324 vel. 14-16 (4).jpg” width=“500” height=“333” title="Majice/Majica �-324 vel. 14-16 (4).jpg /></a><a href=“Majice/Majice/Majica �-324 vel. 14-16.jpg”>
<img src=“Majice/Majice/Majica �-324 vel. 14-16.jpg” width=“500” height=“333” title="Majice/Majica �-324 vel. 14-16.jpg /></a>