Jump to content

[SOLVED] Putting a file directory in a specific order


jerel

Recommended Posts

Hello,

 

I have the code below, looking at the contents of the folder and listing the files, as links to themselves.

I would like to be able to control the way they are sorted. It seems very random.

 

I figured i could write all the file names to an array then have them write to a db, then pull those back from the db. So basically to avoid the extra scripting, is there some way to sort this?

 

thanks

 

??? code below

 

 

$dir = "../archives";

 

$dh = opendir($dir) or die ("could not open dir");

 

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

{

 

if (($file != ".") && ($file != ".."))

{

//echo the link to the file here using the $file variable

echo "<a href='http://www.amimpact.com/archives/$file'>$file</a><br />";

 

}

 

}

Link to comment
Share on other sites

Hello,

 

yes, thanks i also found the array_reverse() very helpful, here is the code if someone else would like to see this finished.

 

$dir = "../archives";

 

$dh = opendir($dir) or die ("could not open dir");

 

$n = "0";

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

{

 

if (($file != ".") && ($file != ".."))

{

$n = $n+1;

//echo the link to the file here using the $file variable

 

$movies[$n] = "$file";

}

}

 

natsort($movies);

$newray = array_reverse($movies);

 

$array_count = count($newray);

 

for($i = 0; $i < $array_count; $i++) {

 

// echo whatever you want using $newray[$i] as your resorted array

echo "$newray[$i]";

 

//phpfreaks.com ROCKS THE HOUSE

 

}

 

?>

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.