PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have an image gallery on my site that I'd like to setup to be a little more dynamic. I'd like to reference my pictures using <?=$pic1?> through <?=$pic24?> how could I dynamincally create these variables based on the files in a folder? So it picks the latest 24, or even the first 24 alphabetically.
Here's an example of how I'm using it:
PHP(This being the part that would change to allow for dynamic naming):
$images = array();
$handle = opendir($path_to_folder);
while ($file = readdir($handle)) { //Read until all files read
$ext = substr($file, -3); //OK there is a better way than this
if ($ext == 'jpg' //File ends with image extension
|| $ext == 'gif'
|| $ext == 'png'
|| $ext == 'bmp') {
$images[] = $file; //Add to array
} //End if
} //End while
closedir($handle);
print_r($images);
Thank you d11wtq, ive learn a lot from your example, and added a few things which i think answers dyonak's question. The code below displays a picture and the pictures filename, of course without the .jpg or .gif extension.
$images = array();
$imgcount = 0;
$handle = opendir('./images');
while ($file = readdir($handle)) { //Read until all files read
$ext = substr($file, -3); //OK there is a better way than this
if ($ext == 'jpg') //File ends with image extension
{
$file = substr($file, 0, -4); //cuts the extension off
$images[] = $file; //Add to array
$imgcount ++;
} //End if
} //End while
while ($imgcount <=24) {
$images[] = zzz;
$imgcount ++;
}
closedir($handle);
This is the final code I went with, seems to work perfectly so far, thanks for the help gang.