Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Php Problem List files in order

Posted on 2013-05-09
3
Medium Priority
?
299 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 111

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 84

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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.
Suggested Courses

618 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