Log in

View Full Version : Playing images in a folder with different pages?



luxuri
11-25-2012, 04:14 AM
Hi all.

Is it possible to do this:
Have a folder setup, for example, /img/r1/
Within that folder will be images in consecutive order.

Then, in my php file, to read those images and display then in a slideshow. However, I will have different php files with different images. For example, I have www.site.com/hey?id=1, is it possible to use this id= and then tell the script to read images from the /r1/ folder, and if I have a page www.site.com/hey?id=5, the script will read images from /r5/ folder?

Thanks.

traq
11-25-2012, 07:25 AM
Of course - you can use the query parameter to specify which folder you want. For security reasons, you should have a a list of allowed folders rather than just allowing any path to be specified.


<?php
$allowedFolders =array( 'r1','r2','r5' );

if(
!empty( $_GET['id'] ) // if the folder I'd is specified
&& in_array( $_GET['id'],$allowedFolders ) // and folder is allowed
){
/* get photos */
}else{
/* do default action (e.g., maybe a menu or something) */
}

luxuri
11-26-2012, 09:07 AM
Hi,

thanks for the reply. I will try this out soon. For the if part " /* get photos */", does that mean to implement a code such as jquery to put in a slideshow?

djr33
11-26-2012, 09:31 AM
There's a PHP slideshow here that has some code that might help you. You can also use a generic code for looping through a folder and grabbing all image files. The exact setup may depend on your system, although it will be simplest if you only have images in the folder and you want to display all of those images. Then you'll just add them using the filepath.

traq
11-26-2012, 02:53 PM
Right - to answer you question specifically, no, not jQuery. PHP works on the server, javascript works on the client (browser). They don't know about each other. With this approach, you'd need to get those files using PHP.



I was looking for the PHP photo album script Daniel mentioned, and I found it, but it's horribly old and uses depreciated (removed in recent PHP versions) functions.

You might use something like this to get all of the images in a particular folder (untested, I haven't used glob() (http://php.net/glob) recently - I'll let you know later if I find problems):
<?php
$allowedFolders =array( 'r1','r2','r5' );

if(
!empty( $_GET['id'] ) // if the folder I'd is specified
&& in_array( $_GET['id'],$allowedFolders ) // and folder is allowed
){
/* get photos */
$dir = $_SERVER['DOCUMENT_ROOT']."path/to/images/{$_GET['id']}/";
foreach( glob( $dir.'*.jpg',GLOB_MARK ) as $jpgImage ){
$imgTags[] = "<img src="/path/to/images/'. $_GET['id'] .'/'. $jpgImage .'" alt="a slideshow image">';'
}
// now $imgTags is an array of <img> elements that you can print out
}else{
/* do default action (e.g., maybe a menu or something) */
}