Page 1 of 4
Sorting by Filetype/Size ?????
Posted: Tue Sep 12, 2006 6:46 pm
by JustinMs66
Everah | Please do not edit the title to show number of pages.
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: Select all
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && !is_dir($file)) {
echo "<a href='" . $file . "'>" . $file . "</a><br/>";
}
}
closedir($handle);
}
?>
Posted: Tue Sep 12, 2006 6:52 pm
by Luke
look up
filesize() and
sort() (and it's friends)
Posted: Tue Sep 12, 2006 6:54 pm
by RobertGonzalez
filesize() and then use an array sorting function.
EDIT | Stinking Ninja beat me to it.
Posted: Tue Sep 12, 2006 6:54 pm
by Weirdan
specifically, usort
Posted: Tue Sep 12, 2006 6:56 pm
by JustinMs66
k i l00ked it up, but i still can't figure out where to put what code... :/
can u help me a little more?
Posted: Tue Sep 12, 2006 7:01 pm
by Luke
while you are looping through the directory, instead of echoing out the name, store the filename, size, and whatever other information into an array and then sort that array with sort, or usort, or whatever... and then loop through THAT array and output the file info.
Posted: Tue Sep 12, 2006 7:02 pm
by JustinMs66
hhhmmmm. i'm not quite getting it. i'm sry, but i'm not the best at php :/
could u give me a little code as an an example?
Posted: Tue Sep 12, 2006 7:04 pm
by Luke
I will when I get home in about 45 minutes... if somebody else hasn't already.
Posted: Tue Sep 12, 2006 7:27 pm
by JustinMs66
k...i'l b waitin

Posted: Tue Sep 12, 2006 8:37 pm
by Luke
Code: Select all
<?php
$directory = "./filebin";
// Function to sort files by size
function sort_by_size($a, $b){
// Compare the two files to see which is larger (you may refer to the usort() documentation to understand how this works)
if ($a['size'] == $b['size']) {
return 0;
}
return ($a['size'] < $b['size']) ? -1 : 1;
}
$dir = dir($directory);
$files = array();
while (($filename = $dir->read()) !== false) {
$file_info = array();
if(is_file($dir->path . "/" . $filename)){
// Gather file info and store it into an array
$file_info['name'] = $filename;
$file_info['path'] = realpath($dir->path . "/" . $filename);
$file_info['size'] = filesize($file_info['path']);
$files[] = $file_info;
}
}
// Sort the array using the function above
usort($files, 'sort_by_size');
// This is just to show that it has been sorted
print_r($files);
?>
Posted: Tue Sep 12, 2006 9:20 pm
by RobertGonzalez
I wrote a little method that takes an array of file extension and allows for them to be read and listed for display/linking. You can use this as a guide as well (though Ninja's looks to be a little more suited to what you are doing).
Code: Select all
<?php
function set_dl_file_types()
{
$this->site_dl_file_types = array(
'pdf' => 'Portable Data',
'doc' => 'Microsoft Word',
'xls' => 'Microsoft Excel',
'txt' => 'Plain Text',
'rdf' => 'Rich Data File',
'rtf' => 'Rich Text Format'
);
return $this->site_dl_file_types;
}
function set_dl_list($dir, $ext_group = '')
{
$file_type_array = $this->set_dl_file_types();
$filelimit = FALSE;
foreach ($file_type_array as $key => $value) {
$file_ext_array[] = '.' . $key;
$file_name_array[] = $value;
}
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$ac = 0;
while ( false !== ($file = readdir($dh)) ) {
if ($file != '.' && $file != '..') {
$filename = str_replace('-', ' ', $file);
$filename = str_replace($file_ext_array, '', $filename);
$filesize = number_format(filesize($dir.$file) / 1000000, 2, '.', ',');
if (!empty($ext_group) && array_key_exists($ext_group, $file_type_array)) {
if (strstr($file, $ext_group)) {
$this->site_dl_list[$ac]['filename'] = $file;
$this->site_dl_list[$ac]['filetext'] = $filename;
$this->site_dl_list[$ac]['filesize'] = $filesize;
$ac++;
}
} else {
$this->site_dl_list[$ac]['filename'] = $file;
$this->site_dl_list[$ac]['filetext'] = $filename;
$this->site_dl_list[$ac]['filesize'] = $filesize;
$ac++;
}
}
}
closedir($dh);
}
}
return $this->site_dl_list;
}
?>
Posted: Tue Sep 12, 2006 9:39 pm
by JustinMs66
everah, i dunno what the F**K u just did, but it's way too complicated. lol. so i'm stickin wit ninja's code.
BUT, ninja, question:
Array ( [0] => Array ( [name] => trial1.doc [path] => /filebin/trial1.doc [size] => 8258 ) )
is what comes up...so like yea. how do i make it less "code-ey" i guess lol.
Posted: Tue Sep 12, 2006 9:42 pm
by RobertGonzalez
Come back in a few months and it will all make perfect sense to you. Maybe.
Posted: Tue Sep 12, 2006 9:43 pm
by Luke
no... my guess is that you passed it an invalid directory... this is probably a given, but did you change the $directory variable to the directory you need it to iterate? also... check whether the directory is a directory before doing any of that code with
is_dir(). I forgot to add that check in there.
EDIT: I wrote the above before you edited your post. To answer your new question... now just do what you did in the first place, iterate through the array (instead of print_r) and echo out the filenames.
Code: Select all
foreach($files as $file){
echo $file['name'];
}
Posted: Tue Sep 12, 2006 9:52 pm
by JustinMs66
ok...yea that works...it displays the file size. but it still won't Sort by file size.