Solved

Php Problem List files in order

Posted on 2013-05-09
3
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 110

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 34

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
update query is not working , giving Mariadb server version error 1 44
help thx 6 22
Find RGB colors from a screen. 2 19
Delete image(s) associated with record(s) 16 29
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
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.

726 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