Jump to content

How do I list folders/files alphabetically HELP!!!


tigertim

Recommended Posts

Hi

 

Sorry if this is the wrong place for this but I'm totally stumped here and I know its a basic question but I'm very new to php and I need help :-)

 

Basically all i would like to do is list the files and folders in the following directory alphabetically, I know i need to put them in an array etc but I don't know how and would really appreciate some help! So any help would be very gratefully received.

 

Here's my code so far:

 

<?php

 

$sub = ($_GET['dir']);

if (strstr($sub, "..")) {$sub = "";}

$path = '../media/Documents';

$path = $path . "$sub";

$dh = opendir($path);

$i=1;

while (($file = readdir($dh)) !== false) {

if(substr($file,0,1) != ".") {

if (substr($file, -4, -3) =="."){

echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i.

$file <br />";

}else{

echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a

href='?dir=$sub/$file'>$file</a><br />";

}

 

$i++;

}

}

closedir($dh);

 

?>

 

Many thanks in advance!

Hi

 

Read the files into an array, then use asort to sort the array and then loop through the resulting array.

 

Something like this

 

 

<?php$sub = ($_GET['dir']);if (strstr($sub, "..")) {$sub = "";}$path = '../media/Documents';$path = $path . "$sub";$dh = opendir($path);$i=1;$fileArray = array();while (($file = readdir($dh)) !== false) {$fileArray[] = $file;}closedir($dh);asort)$fileArray);foreach($fileArray AS $singleFile){if(substr($singleFile,0,1) != ".") {	if (substr($singleFile, -4, -3) ==".")	{		echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i.		$singleFile <br />";	}	else	{		echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a		href='?dir=$sub/$singleFile'>$singleFile</a><br />";	}	$i++;}}?>

 

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.