how to display images in a folder by using php?

posted by sai on 2009-07-01 11:19:53
 
<?
$imglist='';
 
//$img_folder is the variable that holds the path to the banner images. Mine is images/tutorials/
// see that you don't forget about the "/" at the end
 
$img_folder = "C:\xampp\htdocs\images";
 
mt_srand((double)microtime()*1000);
 
//use the directory class
$imgs = dir("C:\xampp\htdocs\images");
 
//read all files from the directory, checks if are images and ads them to a list (see below how to display flash banners)
while ($file = $imgs->read() )
{
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
$imglist = "$file";
 
} closedir($imgs->handle);
 
//put all images into an array
$imglist = explode(" ", $imglist);
$no = sizeof($imglist)-2;
 
//generate a random number between 0 and the number of images
$random = mt_rand(0, $no);
$image = $imglist[$random];
 
//display image
echo "<img src= $image >";
?>
 
dail avatar
dail
12
Hi sai
 
$images = glob('/var/www/*.{jpg,png,gif}', GLOB_BRACE);
foreach ($images as $image)
    echo basename($image) ."<br />";
 
$images has all the images(.jpg .png .gif) of the directory.
49 answers - 0 questions
  Positive      - 1  Negative

methakon avatar
methakon
0
 
<?php
$test=0;
$struct =$scrpt .  "<div style=' ' id='projectgalary' >
<table border='0' cellspacing='0' cellpadding='0'><tr><td></td><td></td><td></td><td></td> ";
$imges="";
$string="";
$fileCount=0;
$filePath='images/imm/'; # Specify the path you want to look in. 
$dir = opendir($filePath); # Open the path
while ($file = readdir($dir)) 
{ 
   # Look at only files with a .php extension
   
    $string = "$file";
	if(substr(strrchr($string, "."), 1)== "jpg" || substr(strrchr($string, "."), 1)== "jpg" || substr(strrchr($string, "."), 1)== "jpeg" || substr(strrchr($string, "."), 1)== "jpg" )
	if($string != "." && $string != "..")
	{
	if(($fileCount%2)==0)
				{
				$imges = $imges . " </tr><tr>";
				}
	$path=$filePath.$string;
	$test++;
	$file_p=fileperms($path);
	$item="<td><a href='" . $path . "' class='highslide' > <img src='" .  $path .
				"' alt='Highslide JS' title='Click to enlarge' width='100' height='100' /> </a></td><td>" . ""
				. "</td>";
 
 
				$imges = $imges . $item;
				$fileCount++;
 
	}
 
 
}
if($fileCount != 0)
{
 $imges=$imges;
}
else
{
$imges="<h1>No file In  galary</h1>";
}
 
$struct = $struct . $imges . "</table>
</div>";
echo $struct;
 
?>
 
 
 
1 answers - 1 questions
  Positive        Negative



Answer the question



Top Users
  • dail (12)
  • livin52 (3)
  • camu (3)
  • softweb (2)
  • Nadine (1)
  • Josware (1)
  • lfelipecr (1)
  • gregoriohc (1)
  • Mitu (1)
  • ryan714 (1)