Filenames to variables [solved]

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!

Moderator: General Moderators

Post Reply
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Filenames to variables [solved]

Post by dyonak »

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):

Code: Select all

<?php
$pic1 = filename;// through pic24...
?>
JS:

Code: Select all

var &lt;?=$pic1?&gt; = new Image();
&lt;?=$pic1?&gt;.src = &quote;http://www.dustinyonak.net/images/&lt;?=$pic1?&gt;.jpg&quote;;
HTML:

Code: Select all

&lt;a href = &quote;http://www.dustinyonak.net/http://www.dustinyonak.net/images/&lt;?=$pic1?&gt;.jpg&quote; onmouseover = &quote;doButtons('&lt;?=$pic1?&gt;')&quote;&gt;&lt;img src=&quote;http://www.dustinyonak.net/images/&lt;?=$pic1?&gt;.jpg&quote; height=&quote;60&quote; width=&quote;80&quote; border=0&gt;&lt;/a&gt;
Thanks!
Last edited by dyonak on Wed Jul 20, 2005 9:46 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Something to get you started:

Code: Select all

$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);
neugent
Forum Newbie
Posts: 24
Joined: Wed Jul 06, 2005 3:35 am
Location: Philippines

Post by neugent »

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. :D

Code: Select all

<?php

$path_to_folder='../items/thumbnails/'; //Change to your thumbnail's directory
$images = array();
$handle = opendir($path_to_folder);
while ($file = readdir($handle)) { //Read until all files read
 
    $ext = substr($file, -3); 
    if ($ext == 'jpg' || $ext == 'gif' || $ext == 'png' || $ext == 'bmp') {
		$images[] = $file; 
    } 
} 
closedir($handle);

foreach($images as $image){
	
	//Counts the length of your filename then subtracts 4 strings, the extension
	//and  the . from the filename.
	$len = strlen($image) - 4;

	$filename = substr($image,0,$len);
	echo "<img src='".$path_to_folder.$image."'><br>$filename<br>";
}

?>
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post by dyonak »

Code: Select all

$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.
Post Reply