Jump to content


Photo

Sorting by Filetype/Size ?????


  • Please log in to reply
7 replies to this topic

#1 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 12 September 2006 - 08:50 PM

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:
        <?php
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && !is_dir($file)) {
           echo "<a href='" . $file . "'>" . $file . "</a><br/>";
       }
   }
   closedir($handle);
}
?>




#2 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 12 September 2006 - 09:39 PM

read all the files into an array first and then sort them according to your desired field and then loop through them and display them.

#3 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 12 September 2006 - 11:47 PM

...english? lol. please explain more, i'm not as knowledgable as you in PHP

#4 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 13 September 2006 - 12:18 AM

change your code this way:
<?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>';
}
?>


#5 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 13 September 2006 - 12:28 AM

hhmmm...when i try that it just displays the files.

but like in your //coments you say:
//access it using 2 indexes like this  echo $thefiles[0]['filetype'];
and i try putting in that code, but nothing happens...i'm not sure what i do with that, if anything.

#6 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 13 September 2006 - 12:53 AM

you have to use the filesystem functions to get the other file info
http://www.php.net/m....filesystem.php
-and put those statements before this statement, setting '$getthisvalue' and '$getthesize'
//use filesystem functions to get the filesize and other attributes RTFM
$thefiles[] = array('filename' => $file, 'filetype' => $getthisvalue, 'filesize' => $getthesize);

-use the statement above as a 'template' and add whatever number of file attributes you want into it


#7 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 13 September 2006 - 01:11 AM

but thats the same line of code as before...OH i think i get it

but like what do i change, the 'filesize' or the $getthesize ? and what do i put in in pace of that?

#8 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 13 September 2006 - 03:12 PM

the $getthesize variable would be something you set before that statement using the filesystem function filesize()
$getthesize = filesize($file);
$thefiles[] = array('filename' => $file, 'filetype' => $getthisvalue, 'filesize' => $getthesize);

In that statement I am creating an associative array with 3 data values.  The template is
array('thenameyougivethefield' => $theactualdatatobestored, [**repeat the format to the left as many times as you want for as many items you want in the array**]);
-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/m....filesystem.php
-good time to give yourself a class in php arrays:
http://www.php.net/m...types.array.php
http://www.php.net/m...n/ref.array.php




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users