Jump to content

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

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.