Jump to content

Sorting by Filetype/Size ?????


JustinMs66@hotmail.com

Recommended Posts

right now i have a working php script that shows all the files in the directory and puts a link on it, but what i want is to not only have the file names, but after the file names (in a different form) have the file type, and then after that have the file size. and eventually i wana be able to click on "file size" and have it sort by file size. is that possible?

here i my current code:
[code]        <?php
if ($handle = opendir('.')) {
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != ".." && !is_dir($file)) {
          echo "<a href='" . $file . "'>" . $file . "</a><br/>";
      }
  }
  closedir($handle);
}
?>[/code]


Link to comment
https://forums.phpfreaks.com/topic/20553-sorting-by-filetypesize/
Share on other sites

change your code this way:
[code]<?php
$thefiles = array(); //initialize
if ($handle = opendir('.')) {
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != ".." && !is_dir($file)) {
            //use filesystem functions to get the filesize and other attributes RTFM
            $thefiles[] = array('filename' => $file, 'filetype' => $getthisvalue, 'filesize' => $getthesize);
            //used this way, it just adds the new item to the end of the array
            // it is creating an array of arrays
      }
  }
  closedir($handle);
}
//at this point the $thefiles array contains all the file info
//access it using 2 indexes like this  echo $thefiles[0]['filetype'];
//use array functions to sort $thefiles array to your pleasure RTFM
//use the foreach() to cycle through the $thefiles array and echo the files and other attributes out
foreach ($thefiles as $singlefile) {
    echo $singlefile['filename'] . ' ' . $singlefile['filetype']
      . ' ' . $singlefile['filesize'] . '<br>';
}
?>[/code]
hhmmm...when i try that it just displays the files.

but like in your //coments you say:
//access it using 2 indexes like this  [b]echo $thefiles[0]['filetype'];[/b]
and i try putting in that code, but nothing happens...i'm not sure what i do with that, if anything.
you have to use the filesystem functions to get the other file info
http://www.php.net/manual/en/ref.filesystem.php
-and put those statements before this statement, setting '$getthisvalue' and '$getthesize'
[code]//use filesystem functions to get the filesize and other attributes RTFM
$thefiles[] = array('filename' => $file, 'filetype' => $getthisvalue, 'filesize' => $getthesize); [/code]
-use the statement above as a 'template' and add whatever number of file attributes you want into it
the $getthesize variable would be something you set before that statement using the filesystem function filesize()
[code]$getthesize = filesize($file);[/code]
[code]$thefiles[] = array('filename' => $file, 'filetype' => $getthisvalue, 'filesize' => $getthesize); [/code]
In that statement I am creating an associative array with 3 data values.  The template is[code] array('thenameyougivethefield' => $theactualdatatobestored, [**repeat the format to the left as many times as you want for as many items you want in the array**]);[/code]
-you would set the value the value of $theactualdatatobestored yourself before reaching this statement.  Use the filesystem section of the php doc to find the functions you need.
http://www.php.net/manual/en/ref.filesystem.php
-good time to give yourself a class in php arrays:
http://www.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/ref.array.php

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.