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

Post by Luke »

show me your code... all of it. Image
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

k here:

Code: Select all

<?php
$directory = "./filebin";
function sort_by_size($a, $b){
    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;
          //My Echo Code:
                echo $file_info['name'] = $filename;
				echo "<br>";
                echo $file_info['size'] = filesize($file_info['path']);
				echo "<br>";
                echo $files[] = $file_info;
				echo "<br><br>";
        }
}
usort($files, 'sort_by_size');
?>
and i created a "filebin" folder too.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

It's because you echoed out the names before you sorted the array... this should do it.
JustinMs66 wrote:k here:

Code: Select all

<?php
$directory = "./filebin";
function sort_by_size($a, $b){
        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;
        }
}
usort($files, 'sort_by_size');
foreach($files as $file){
    echo $file['name'] . "<br>";
}
?>
and i created a "filebin" folder too.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

k that works, thnx man!

but like...how would i make that a link? like the user would click on "size" and it would sort by size... how?

also... how would i make the same kinda script for "type"
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Well I'm not going to write any more php cuz I got stuff to do, but here are some things you will need to know about...

For making the link:
This superglobal contains an array of the variables passed in the url (http://www.yourdomain.com/index.php?action=sort_by_size)
$_GET

For sorting by file type... I may not know the best way to do this... the first thing that comes to mind to me is to get the extension with explode() and then write a function to sort by the extension (like the one I wrote for file size)
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

k with the explode() thing i was able to do this:

Code: Select all

<?php
$str1 = "FileNameExample.Extention"; 
$str3 = explode('.', $str1 , 2 ); 
echo $str3[1]
?>
which outputted this:
Extention

which is perfect, because that is the part that needs to be recognised as the extension....the only problem is that it will always be "FileNameExample.Extention" that it uses, unless i say otherwise

my ONLY question, is how do i get "$str1" to equal the files in a directory?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I have no problem with you asking questions... as long as you are trying to learn and not just get the code written for you.

Do the same thing as you did to output the file name, but instead, process its extension:

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']);
		
	        $apart = explode(".", $filename);
	        $key = count($apart)-1;
	        $ext = $apart[$key];
		$file_info['ext'] = $ext;
		$files[] = $file_info;
	}
}

// Sort the array using the function above
usort($files, 'sort_by_size');

// Reverse
$files = array_reverse($files);

// This is just to show that it has been sorted
print_r($files);
?>
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

hmmm.... awesome dude. i just got sum quesitons. i did everything i could.

and so like, at the end, instead of this:
print_r($files);

i put this:
echo $file['ext'] . "<br>";

but that dosn't work, it just comes up with a blank screen. cuz i'm trying to get rid of the code, just like last time.

so how would i say it so it works?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

foreach foreach foreach!! :P :D

Code: Select all

foreach($files as $file){
    echo "<h1>" . $file['name'] . "</h1>";
    echo "<b>Extension:</b> " . $file['ext'] . "<br>";
    echo "<b>Size:</b> " . $file['size'] . "<br>";
    echo "<b>Path:</b> " . $file['path'] . "<br>";
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

$files is an array. Do a print_r() or var_dump() of the array to see what is in it, then use that patter to loop the array for echo'ing.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok awesome thnx man!

and this is what comes of it:
  • file.psd
    Extension:psd

    file.html
    Extension:html

    file1.php
    Extension:php

    file2.php
    Extension:php

    file.gif
    Extension:gif
which is perfect and awesome, but that isn't exactly sorted... is it?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

it's sorted by file size
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

Code: Select all

:( wait, but why? i thought we were doing it by type!

hmm.... so i changed the code to this:

<?php
$directory = "./uploads";
// Function to sort files by type
function sort_by_type($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['type'] == $b['type']) {
                return 0;
        }
        return ($a['type'] < $b['type']) ? -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']);
               
                $apart = explode(".", $filename);
                $key = count($apart)-1;
                $ext = $apart[$key];
                $file_info['ext'] = $ext;
                $files[] = $file_info;
        }
}
usort($files, 'sort_by_type');
$files = array_reverse($files);
	foreach($files as $file){

    echo $file['name'] . "<br>";
    echo "Extension:" . $file['ext'] . "<br><br>";
}
?>
which outputs this instead:

file1php
Extension:php

file2.php
Extension:php

file.gif
Extension:gif

file.html
Extension:html

file.psd
Extension:psd

... and is that sorted by type? i can't really tel... cuz it's definaltey not by alphabetal type.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're gonna need to use some logic here. How is PHP going to know which file type it is? How are you going to want those types sorted? These questions will put you on the road to modifying the dort_by_type function to meet your needs.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

And also, no need to edit your thread titles to indicate how many pages the thread contains, we don't need to know that.
Post Reply