• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

Php Problem List files in order

Hey everybody!

I have this code here that shows images from a directory. Right now it seems to displaying the images in random order. Right now the images are numbered 01.jpg, 02.jpg etc. Could someone help me add code to mine and to display them in order?

<?php

function list_files($dir)
{
  if(is_dir($dir))
  {
  	echo '<div id="gallery" class="content">
				<div id="controls" class="controls"></div>
				<div class="clear"></div>
				<div class="slideshow-container">
					<div id="loading" class="loader"></div>
					<div id="slideshow" class="slideshow"></div>
				</div>
			</div><!-- gallery -->
	
			<div id="thumbs" class="navigation">
				<ul class="thumbs noscript">'."\n";
    if($handle = opendir($dir))
    {
      while(($file = readdir($handle)) !== false)
      {
        if($file != "." && $file != ".." && $file != "Thumbs.db" && strtolower(substr($file,strlen($file)-3,3))!='jpg'  && strtolower(substr($file,strlen($file)-3,3))!='gif' /*pesky windows, images..*/)
        {
		  echo '<li><a class="thumb" href="'.$dir.$file.'"><img src="'.$dir.$file.'" alt="" /></a></li>'."\n";

        }
        if($file != "." && $file != ".." && $file != "Thumbs.db" && strtolower(substr($file,strlen($file)-3,3))=='jpg' xor strtolower(substr($file,strlen($file)-3,3))=='gif' /*pesky windows, images..*/)
        {
		  echo '<li><a class="thumb" href="'.$dir.$file.'"><img src="'.$dir.$file.'" alt="" /></a></li>'."\n";
        }
      }
      echo '</ul>
			</div><!-- thumbs -->';
      closedir($handle);
    }
  }
}

?>

Open in new window

0
kk89
Asked:
kk89
1 Solution
 
Ray PaseurCommented:
PHP scandir() will get you the list in a predictable order.  Then you can access the elements of the array to build the HTML document.  If you need to sort the array, this page will give some guidance.
http://www.php.net/manual/en/array.sorting.php
0
 
Dave BaldwinFixer of ProblemsCommented:
Here's a PHP page I use to list 'executable' HTML files in sorted order on a server.  You can change the extensions in $extstr to image extensions like 'jpg' and 'gif'.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head><title>PHP Directory Listing</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">
<!--  
body { 
	margin: 10px;
	background-color: #ffffff;
	font-family: Arial;
	color: #000000;
	}
a {
	font-family: Times New Roman, Times, serif; 
	text-decoration: none; 
	font-weight: bold;
	}	
a:link {color: #000000;text-decoration: none;}     /* unvisited link */
a:visited {color: #333333;text-decoration: none;}  /* visited link */
a:hover {color: #886622;text-decoration: underline;}   /* mouse over link */
a:active {color: #cc0000;text-decoration: none;}   /* selected link */

h1 { 
	font-family: Times New Roman, Times, serif; 
	margin: 1px;
	}		 
-->
</style>
</head>

<body>

<h1>PHP Directory Listings</h1>

<?php
$extstr = " php html asp aspx";
$parts = array();
$listing = array();
$dirs = array();

if ($handle = opendir('.')) {
		$j = 0;
    while (false !== ($dlist[$j] = readdir($handle))) {
        //if ($file != "." && $file != "..") $j++;
        if ($dlist[$j] != "." && $dlist[$j] != "..") $j++;
				}
		//sort($dlist, SORT_STRING);
		natcasesort($dlist);
		foreach ($dlist as $key => $val) {
		$parts = explode(".",$val);
		if(isset($parts[1]) && (strpos($extstr, $parts[1]) > 0)) {
			if(!isset($parts[2])) {
    		$listing[] = $val;
				}
			}
		if(is_dir($val)) $dirs[] = $val;
		}
    closedir($handle);
		//sort($listing, SORT_STRING);
		natcasesort($listing);
		$Lencnt = count($listing);
		$clng = 27;
		$divsr = ceil($Lencnt/$clng);	
		//$divsr = ($Lencnt/$clng);	
		echo $Lencnt." : ".$divsr."<br>\n";
		$Lenlist = $Lencnt/$divsr;
		echo $Lenlist."<br>\n";
		echo '<table border="1" cellpadding="7px" cellspacing="0" summary=""><tr><td valign="top">';
		foreach ($listing as $val) {
   		echo '<a href="' . $val . '">' . $val . "<br>\n";
			$Lenlist = $Lenlist-1;
			if($Lenlist < 0) {
				echo '</td><td valign="top">';
				$Lenlist = $Lencnt/$divsr;
				}
		}
		echo '</td><td valign="top"><b><u>Directories</u></b><br><br>';
		$Lenlist = $Lencnt/$divsr;
		foreach ($dirs as $val) {
   		echo '<a href="' . $val . '">' . $val . "<br>\n";
			$Lenlist = $Lenlist-1;
			if($Lenlist < 0) {
				echo '</td><td>';
				$Lenlist = $Lencnt/$divsr;
				}
		}
		
		echo '</td></tr></table>';
	}
?> 

</body>
</html>

Open in new window

0
 
Slick812Commented:
greetings  kk89, , below is some code that sorts the image files alphabetically and puts them in a list -
$dir = 'images3/';
if(is_dir($dir)) {
    if($files = scandir($dir)){
    $files = array_diff($files, array('..', '.'));
    echo 'LIST<ul>';
    foreach ($files as $file) {
        $ext = substr($file, -4);
        if(($ext=='.gif') || ($ext=='.jpg') || ($ext=='.png') || ($ext=='jpeg') || ($ext=='.svg'))
        echo '<li><a class="thumb" href="'.$dir.$file.'"><img src="'.$dir.$file.'" alt="" /></a></li>'."\n";
        }
    echo '</ul>';
    }
    }

Open in new window


this code works for me on my server, ask questions, if it is not doing what you need.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now