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 »

there is no 'type' key in the array $files... try 'ext' instead
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

oh, yea, DUH LOL thank you, yes that worked :)))

and so if i just incorperate the php sorting code into my index.php file and just say like:
http://www.mywebsite.com/index.php?action=sort_by_size
or
http://www.mywebsite.com/index.php?action=sort_by_type

it will work? correct?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

nope... you still have to determine which action was sent:

Code: Select all

switch($_GET['action']){
    case: 'sort_by_type':
        usort($files, 'sort_by_type');
        break;
    case: 'sort_by_size':
        usort($files, 'sort_by_size');
        break;
    default:
        echo "No sorting";
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You know, you might be able to tweak array_multisort() to your liking...
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

huh. well i tried putting in that code, and it's saying:
Parse error: syntax error, unexpected ':' in /3.php
and for that, would i use the "include" thing for this?

and also... just for the listing and Not for the actual sorting, i can't get the "type" code to work with my "index.php" :( i just used that code we made for sorting... and it dosn't work. it says something like "invalid dir" and "invalid string". :(
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Show us your code... show us your exact errors
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

JustinMs66 wrote:huh. well i tried putting in that code, and it's saying:
Parse error: syntax error, unexpected ':' in /3.php
and for that, would i use the "include" thing for this?

and also... just for the listing and Not for the actual sorting, i can't get the "type" code to work with my "index.php" :( i just used that code we made for sorting... and it dosn't work. it says something like "invalid dir" and "invalid string". :(
When you have a 'Parse error:' message PHP is telling you that there is a problem with the way you wrote your code. It tells you what the problem is. It also tells you exactly where in your code the problem is. Try looking at the error message on your screen again then check the script and see if you can figure out what is happening.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

well for just the listing part, i just inserted the code we made for sorting by file TYPE into the index.php. but anyway, here it is. it's kinda lot of code... :/

ok, all of this code is inside the index.php file, exactly like this:

Code: Select all

<?php
	//php code for listing the title of all files in the directory.
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && $file != "index.php" && $file != "upload.php" && $file != "index.html" && !is_dir($file)) {
           echo "<a href='" . $file . "'>" . $file . "</a><br/>";
       }
   }
   closedir($handle);
}
?>

Code: Select all

<?php
	//php code for switching:
switch($_GET['action']){
    case: 'sort_by_type':
        usort($files, 'sort_by_type');
        break;
    case: 'sort_by_size':
        usort($files, 'sort_by_size');
        break;
    default:
        echo "No sorting";
}
?>


Code: Select all

<?php
	//tje php code that we made for sorting by file type.
	//i thought it would work with this
$directory = "./uploads";
// Function to sort files by type
function sort_by_ext($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['ext'] == $b['ext']) {
                return 0;
        }
        return ($a['ext'] < $b['ext']) ? -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_ext');
// This is just to show that it has been sorted
	foreach($files as $file){
    echo $file['name'] . "<br>";
    echo "Extension:" . $file['ext'] . "<br><br>";
}
?>

Code: Select all

<?php
	  //php code for listing the file size of the directories
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && $file != "index.php" && $file != "upload.php" && $file != "index.html" && !is_dir($file)) {
//	   $fruits = array($file);
//	   sort($fruits);
           echo filesize($file) . ' bytes' . "<br/>";
       }
   }
   closedir($handle);
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ok, all of this code is inside the index.php file, exactly like this:
then why did you put it in four

Code: Select all

blcoks here?

The first block echos the filenames immediatly. Is it unconditionally executed before the other three blocks?
The last block immediatly echos the filesize of all files in the current directory.
And the two in the middle do not make much sense ..at least in this order.
Sorry, it's all pellmell. Please describe in your own words what each block does and what you have and need before and what you have after it is executed.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

how come you keep changing the thread title to display how many pages it has? We don't need to know that... and if we did need to know that, we could find out easily because it displays how many pages the thread is right next to it (without you typing it)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I've removed the total pages from the thread title and posted a little request in the original thread asking the OP to not add the number of pages to the thread title.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok i put 4

Code: Select all

blocks in because i have 4 different sections of PHP in my index.php file, so i was just trying to show that.

and i explain what each block is with a //comment at the top of each block. they are all from different files, and i am trying to put them all together into 1 file. but the sorting ones, i only want to be there, because they wont be used unless i call them with http://www.myurl.com/index.php?action=sort_by_size or something liek that.

so can i plz know how to do this?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

you've put them all in different pages? How do you expect all the pages to be concious of $files??
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

...no. i HAD them in different pages, and how i'm trying to all put them in ONE page, so that they can be called.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Well no offense, but we've given you all the information you need to figure it out... so you should study up on all the functions we have referred to and used thoughout this thread and just in case you don't know about it...

PHP has some of the best documentation I've ever seen
Post Reply