Jump to content

Get images from image folder


Recommended Posts

This is a script I am trying to create for my portfolio. I already part of it working. I can go get the images from the image folder no problem. I want to go a bit further than that. Here is an example of what is in that folder:site1_small.jpgsite1_large.jpgsite2_small.jpgsite2_large.jpgAnd so on...My php page contains something like this: <a href="site1_large.jpg"><img src="site1_small.jpg" alt="description" /></a>My problem is how can I be sure the href and src will be the corresponding small and large image? What about the description? I currently have it looping through all the images in that folder. I was thinking of having an array to store the small images, large images and the image description.Here's what I got:

<?php			//Open images directory			$dir = @ dir("../images/websites");						//List files in images directory			while (($images = $dir->read()) !== false) {				if(strstr($images, "_small")) {					echo "<li><a href=\"/images/websites/site1_large.jpg\"><img src=\"/images/websites/" . $images . "\" alt=\"desription\" /></a></li>";				}			}			$dir->close();			?>

I'd want to avoid using a db for this. Any ideas?

Link to post
Share on other sites

HiHere's two ways you can do this:Instead of printing them out directly, store them in an array, then make sure that the array is sorted using sort( $files ).Then, unless there's a file called site2_link.jpg or somthing like that, the filenames site2_large.jpg and site2_small.jpg lies after eachother and you can use something like this

for( $i = 2; $i < count($files); $i+=2 ) {	 echo "<li><a href=\"/images/websites/".$files[$i+1]."\"><img src=\"/images/websites/" . $files[$i] . "\" alt=\"desription\" /></a></li>"; }

Or you can check if the file contains small or large in the loop that gets the files and store them in two different arrays.For the description you can use a db (but that's not an easy way), you can use a text-file that contains each site (site1, site2 etc) on a line (or for example comma seperated) followed by the description or you can create one file for each site that has the decription. If you choose the last one you need to create a second (or third) array for those files, pref. using hash ($desc_files[$site] = $desc_file, where $site contains site1 etc) and then get the descriptions from each file using the filenames in $desc_files.Hope that helped..Good Luck and Don't Panic!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...