Solved

Php Problem List files in order

Posted on 2013-05-09
3
293 Views
Last Modified: 2013-11-19
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
Comment
Question by:kk89
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39154054
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39154248
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
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 39154474
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
The viewer will learn how to dynamically set the form action using jQuery.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now