Sorting by Filetype/Size ?????

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Sorting by Filetype/Size ?????

Post 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);
}
?>
Last edited by JustinMs66 on Wed Sep 13, 2006 7:13 pm, edited 2 times in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

look up filesize() and sort() (and it's friends)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

filesize() and then use an array sorting function.

EDIT | Stinking Ninja beat me to it.
Last edited by RobertGonzalez on Tue Sep 12, 2006 6:54 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

specifically, usort
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I will when I get home in about 45 minutes... if somebody else hasn't already.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

k...i'l b waitin :wink:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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);
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
	}
?>
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Come back in a few months and it will all make perfect sense to you. Maybe.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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'];
}
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok...yea that works...it displays the file size. but it still won't Sort by file size.
Post Reply