Jump to content

list file in order


leequalls

Recommended Posts

Right now I am displaying a list of files in a dir. the files do not output in any type of order I would like them to display either alphabetically or by upload date. I have posted my code below any help would be great.

 

<table border=0><tr><td align=center><b>File</b></td><td><b>Remove</b></td></tr>
<?php
$remove = $_GET[remove];
$dir = "/home/public_html/upload/files/";
$remove = $dir."".$remove;
if($remove != "/home/public_html/upload/files/") {unlink($remove);}


// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) { $color = "gray";
        while (($file = readdir($dh)) !== false) {
            if($color == "gray") { $color = "black"; }
            elseif($color == "black") { $color = "gray"; }
            if(filetype($dir . $file) == "file") { echo "<tr bgcolor=$color><td><a href='../upload/files/$file'>$file</a></td><td align=center><a href='?remove=$file'>X</a></td></tr>"; }
        }
        closedir($dh);
    }
}
?>
</table>

Link to comment
https://forums.phpfreaks.com/topic/212933-list-file-in-order/
Share on other sites

To sort them alphabetically you'll need to first need to add all the files to an array, then use asort to sort them into alphabetical order.

To output the files you'll use a foreach loop to iterate through the array of files.

 

<?php
$remove = $_GET[remove];
$dir = "/home/public_html/upload/files/";
$remove = $dir."".$remove;
if($remove != "/home/public_html/upload/files/") {unlink($remove);}


// Open a known directory, and proceed to read its contents
if (is_dir($dir)){
    if ($dh = opendir($dir)) {

        $files = array();
        while (($file = readdir($dh)) !== false) {
        
            if(filetype($dir . $file) == "file") {
                // add the file to the files array
                $files[] = $file;
            }
        }
        closedir($dh);
    }
}

// sort the files alphabetically
asort($files);
?>

<table border=0>
  <tr>
    <th>File</th>
    <th>Remove</th>
  </tr>
<?php
// Output the files
$i = -1;
foreach($files as $file):
    $color = (++$i % 2 == 0): 'grey' : 'black';
?>
  <tr bgcolor="<?php echo $color; ?>">
       <td><a href="../upload/files/<?php echo $file; ?>"><?php echo $file; ?></a></td>
       <td align=center><a href="?remove=<?php echo $file; ?>">X</a></td>
  </tr>
<?php endforeach; ?>
</table>

 

I would like them to display either alphabetically or by upload date.

You may want to look at the filemtime function

Link to comment
https://forums.phpfreaks.com/topic/212933-list-file-in-order/#findComment-1109121
Share on other sites

Hi all,

 

Please correct my if I am wrong, but if error_reporting(E_ALL) was used in this instance, it would highlight an error on the use of $_GET[remove], wouldn't it? (Shouldn't it be $_GET['remove'])

 

It would error with something like " undefined index remove presumed constant on line x"

 

Only reason I ask is because I have always understood that referencing an global ($_POST/$_GET/$_FILES etc) you need to use quotes so that the parser doesn't think as it is handling a constant?

 

Just wondering there...

 

Cheers,

Rw

Link to comment
https://forums.phpfreaks.com/topic/212933-list-file-in-order/#findComment-1109124
Share on other sites

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.