Page 2 of 4

Posted: Tue Sep 12, 2006 9:58 pm
by Luke
show me your code... all of it. Image

Posted: Tue Sep 12, 2006 10:05 pm
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.

Posted: Tue Sep 12, 2006 10:13 pm
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.

Posted: Tue Sep 12, 2006 10:48 pm
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"

Posted: Tue Sep 12, 2006 11:04 pm
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)

Posted: Tue Sep 12, 2006 11:27 pm
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?

Posted: Tue Sep 12, 2006 11:37 pm
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);
?>

Posted: Tue Sep 12, 2006 11:55 pm
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?

Posted: Wed Sep 13, 2006 12:16 am
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>";
}

Posted: Wed Sep 13, 2006 12:36 am
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.

Posted: Wed Sep 13, 2006 12:26 pm
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?

Posted: Wed Sep 13, 2006 12:34 pm
by Luke
it's sorted by file size

Posted: Wed Sep 13, 2006 12:36 pm
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.

Posted: Wed Sep 13, 2006 12:47 pm
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.

Posted: Wed Sep 13, 2006 12:55 pm
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.