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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.