Jump to content

PHP automated gallery


IvanS

Recommended Posts

Hello!

 

I'm fairly new to PHP and I've been trying to "automate" my gallery, so when i FTP images in certain folders, the script can use those images and put them into the gallery automatically.

Here is the 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've run into 3 problems:

1. When i specify the directory path like this: $directory = "../images/NY/" it doesn't work but in the html file it works perfectly. The script is located in  "root/gallery" , and the images are located in "root/images/NY". But when i move the script to "root/images" and specify the path like this $directory = "NY/" it works like a charm.

 

2. Instead of giving me the following code for each image respectively:

<a href="NY/image1.jpg">
<img src="NY/image1.jpg" width="500" height="333" title="image1" /></a>

 

i get this part first for every image respectively:

 

[0]=>
string(37) "NY/image1.jpg" 

How can i get rid of that part?

 

and after that this:

 

<a href="NY/NY/image1.jpg">
            <img src="NY/NY/image1.jpg" width="500" height="333" title="NY/image1.jpg /></a>

Why does the $filename write out both the path of the folder and the name of the image?

 

3. How can i avoid the problem of special characters? I'm in Croatia, and letters like č, ć, đ, š, ž  are frequently used, and I've used those in naming the files and folders in which i keep the images. For the sake of someone that will ( i hope at least ) help me, I've used NY and image1 as names, but in reality the names consist of the special characters. So is there a way to make php read those normally or do i have to use the standard ones?

 

Thank you very much in advance!

Link to comment
https://forums.phpfreaks.com/topic/260250-php-automated-gallery/
Share on other sites

Well i considered that idea, but since I'm new to PHP, I try to learn as much as I can from the things i don't understand. When I finally make this script as it should be, I'll probably make databases and use info for PHP from them, but until I finish this I won't.

I've made a slight configuration, so the data becomes correct.

 

<?php
$directory = "Majice/";

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

foreach (glob($directory . "*.jpg") as $pathinfo) {

               echo '<a href="' . $pathinfo . '">
                       <img src="' . $pathinfo . '" width="500" height="333" title="' . basename($pathinfo, ".jpg") . '"/></a>';
}
?>

 

but i still have 2 questions.

1. How can i get rid of this in the beginning:

 

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" } 

 

2. Why doesn't the script work when there are special characters in the directory path. For example $directory = "../dječje/majice/" it doesn't work, but for $directory = "../djecje/majice/" it works. How can i avoid renaming the names of the folders?

 

Thank you!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.