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
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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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