Solved

Php Problem List files in order

Posted on 2013-05-09
3
291 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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

763 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

10 Experts available now in Live!

Get 1:1 Help Now