0
$dir = opendir ($path);
        while (false !== ($file = readdir($dir))) {
                if (strpos($file, '.png',1)) {
                    echo "$file <br />";
                }
        }

super short code that lists the .png filenames
obviously the $path variable is the path to the directory.

how can i list them instead in abc order cause of got an order that is not abc!

5
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by mschroeder
0
//put your file in array
$narray[$i]=$file;
//sort it
rsort($narray);
//list it
for($i=0; $i<sizeof($narray); $i++)
echo $narray[$i];
{
0
<?php ob_start("ob_gzhandler"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/TR/REC-html40">
<head>
</head>
<body><a name='top'></a>
<p style='position:fixed; top:auto; right:0; left:auto; Bottom:0;'>
<a href='..' target='_top'>goto Top</a>
</p>
<?php 
$path="whatever";
$p = split('/', $_SERVER['SCRIPT_FILENAME']);
$script_name = $p[count($p)-1];
$dir_handle = @opendir($path) or die("Unable to open $path");
Function get_Extension($m_FileName){
$path_parts = pathinfo($m_FileName);
if ($path_parts["extension"]) {
$m_Extension = strtolower($path_parts["extension"]);
return(strtoupper($m_Extension)); }
else { return "unknown"; } }
function check_image($filename){
$temp=strtoupper(get_Extension($filename));
if($temp=="PNG")) /*if(($temp=="PNG")||($temp=="JPG")) multiple filetypes */
return (true);
else
return (false); }
 Function get_Files($path) {
 if ($handle = opendir($path)) {	
 while (false !== ($file = readdir($handle))) { 
 if(!is_dir($file) && substr($file,O,1) != "."){				
$m_Files[]=$file;
}}
closedir($handle);	}
if(sizeof($m_Files)>1)
asort($m_Files);
return $m_Files; }
 $files=get_Files($path); 
 $filter_files=array_filter($files,"check_image");
 $maxnr=sizeof($filter_files)-1;
 sort($filter_files);
for ($i=0;$i<sizeof($filter_files);$i++){ echo "$filter_files[$i] <br />"; }
closedir($dir_handle); 
 ?>
</p>
</body></html>
<?php ob_flush(); ?>

Edited by almostbob: n/a

0

thanks this seems like the easiest way but where exactly do i put this code? how do incormporate it into my code i posted originally? any help would be appreiciateed.

//put your file in array
$narray[$i]=$file;
//sort it
rsort($narray);
//list it
for($i=0; $i<sizeof($narray); $i++)
echo $narray[$i];
{
0

thanks this seems like the easiest way but where exactly do i put this code? how do incormporate it into my code i posted originally? any help would be appreiciateed.

$dir = opendir ($path);
rsort($dir);
while (false !== ($file = readdir($dir))) {
      if (strpos($file, '.png',1)) {
       echo "$file <br />";
      }
}

I dont really have time to test but if this doesnt work then instead of echo, add $file to another array and then sort and printr it.

Edited by liamfriel: n/a

0

You could also use a PHP 5 DirectoryIterator from the SPL library.

$iterator = new DirectoryIterator('/path/to/directory/');

foreach($iterator as $fileinfo){
	if( !$fileinfo->isDot() && $fileinfo->isFile() ){
		echo $fileinfo->getPathname();
	}
}

DirectoryIterators implement the SplFileInfo class (http://www.php.net/manual/en/class.splfileinfo.php) so anything available there is also immediately available in $fileinfo

From my quick tests and observations this always iterates over a directory alphabetically a -> z but documentation says it does not do any sorting prior.

If you wanted to make it sortable I can show you what a sortable iterator would look like since a DirectoryIterator is a little more "special" then your other iterators.

Edited by mschroeder: added info about default sorting behavior

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.