
Sorting by Filetype/Size ?????
Moderator: General Moderators
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
k here:
and i created a "filebin" folder too.
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');
?>It's because you echoed out the names before you sorted the array... this should do it.
JustinMs66 wrote:k here:and i created a "filebin" folder too.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>"; } ?>
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
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)
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)
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
k with the explode() thing i was able to do this:
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?
Code: Select all
<?php
$str1 = "FileNameExample.Extention";
$str3 = explode('.', $str1 , 2 );
echo $str3[1]
?>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?
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:
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);
?>- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
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?
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?
foreach foreach foreach!!
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>";
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
$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.
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
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>";
}
?>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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England