...

View Full Version : Displaying images from folder



martynball
03-07-2010, 10:31 PM
I want this script to display two images and then put the next two on a new line. But for some reason the <"br />" tag has been echo'd a load of times although the reasults are not JPG's...



//Display images in cars folder
$name = strtolower(G_NAME); //Converts name to lowercase as PHP is case sensitive!
$dir = "images/cars/".$name; $open = opendir($dir); //Get directory location and open it
$i=0;
while ($file = readdir($open)) { //Read files inside folder
if ($file == false) { echo "Empty!"; }
$nfile = explode(".",$file); //Check that the result is a image (jpg).
if ($nfile[1] == "jpg" || "JPG") { echo "<img src=\"images/cars/$name/$file\" width=\"50px\">";
$i++; }
if ($i <= 2) { echo "<br />"; } //Adds new line after two images
}


Result in HTML


<!--Content goes here-->
Audi
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<img width="50px" src="images/cars/audi/.">
<br>
<img width="50px" src="images/cars/audi/image2.JPG">
<br>
<img width="50px" src="images/cars/audi/..">
<img width="50px" src="images/cars/audi/image3.JPG">
<img width="50px" src="images/cars/audi/image1.JPG">
<img width="50px" src="images/cars/audi/image4.JPG">
</div>


I have tryed


if (is_file($file)) { echo "blahblah"; }


Dunna work, just displays nothing...

Dormilich
03-07-2010, 10:42 PM
<!--Content goes here-->
Audi
<br> // $i = 0 <=2 (true)
<br> // $i = 0 <=2 (true)
<br> // etc.
<br>
<br>
<br>
<br>
<img width="50px" src="images/cars/audi/."> // <-- current directory
<br> // $i = 1 <=2 (true)
<img width="50px" src="images/cars/audi/image2.JPG">
<br> // $i = 2 <=2 (true)
<img width="50px" src="images/cars/audi/.."> // <-- parent directory
<img width="50px" src="images/cars/audi/image3.JPG">
<img width="50px" src="images/cars/audi/image1.JPG">
<img width="50px" src="images/cars/audi/image4.JPG">
</div>
further, there is no differenciation between files and folders. and you insert a break if $i is less then 3 (which is not what you’d translate into "after every 2 images") you’ll need the modulo operator for that.

Dormilich
03-07-2010, 10:47 PM
/home/<user>/public_html/images/

I doubt, that a browser can access /home/<user>. I could imagine that /images/img.jpg could work (given public_html is the web root) or use <base> to make any relative paths absolure.

martynball
03-07-2010, 10:48 PM
I don't know what the absolute path is. How can I stop the parent directory and the current directory from showing?

And I will see what happens if I change it to if($i == 2)

Dormilich
03-07-2010, 10:52 PM
I don't know what the absolute path is.

???


How can I stop the parent directory and the current directory from showing?

read in readdir()ís manual entry (http://php.net/readdir), there are all kinds of applications described


And I will see what happens if I change it to if($i == 2)
exactly 1 break after the second hit. I already said, that you should use the modulo operator (%).

martynball
03-07-2010, 10:56 PM
So for the modulo thing, do I do this:


if ($i % 2) { echo "<br />"; }

Dormilich
03-07-2010, 10:57 PM
you’re not serving XHTML, thus

if ($i % 2) { echo "<br>"; }

martynball
03-07-2010, 11:19 PM
Got this from the link you give me:


if ($handle = opendir($dir)) {
while (false !== ($file=readdir($handle))) {
if ($file != "." && $file != "..") {
$i++;
echo "$file";
if ($i % 2) { echo "<br>"; }
}
}
closedir($handle);
}


Works, cheers :) And half of the "<br>" tags where caused by another problem, so that is okay. All working now cheers :)

I don't understand how this works though, please can you break it down for me?


while (false !== ($file=readdir($handle))) {

Dormilich
03-08-2010, 06:46 AM
readdir() works similar to (e.g.) mysql_fetch_assoc() it returns a file/dir name as long as there are some left, if not it returns false (which you have explicitly have to test (including a type test))

Pcfr43k
03-26-2010, 04:09 PM
and what I don't see coming back in the answers of empty results:


if ($file != '')

Just for the people who missed it. ;)

mlseim
03-26-2010, 04:31 PM
This is from another post ...
You grab only the specific filetypes you want, and it puts them into an array.
From the array, you can shuffle it, sort it, pick away at it, whatever you want.



<?php

// directory name where they are stored.
$dir_name="photos";

// put these types of images into an array.
$images = array_merge(
glob("$dir_name/*.jpg"),
glob("$dir_name/*.png"),
glob("$dir_name/*.gif")
);

// Randomize the array.
shuffle($images);

// Display the first 5 random images.
echo"
<img src='$images[0]' /><br />
<img src='$images[1]' /><br />
<img src='$images[2]' /><br />
<img src='$images[3]' /><br />
<img src='$images[4]' /><br />
";

// If you would rather display all of them.
//foreach($images as &$img){
//echo "<img src='$img' /><br />";
//}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum