Jump to content

Help needed sorting file by type, name or size.


FVxSF

Recommended Posts

My roommate and I have a web server set up as an intranet. The idea to develop a server for lan parties so that our buddies can browse files and download files instead of having to ask us where the folders and file are. I have written a script that reads the current directory and displays the File/Folder Name and size but I'm trying to make something a little more advanced. These file are NOT in a mySQL database for the record.

 

What I am trying to do is create a sort order; by name, by type, by size. But I'm having a few problems.

First, when sorting by type, if sorting by type descending, I'd like the folders to be grouped first then sort by the file's extension. And if sorted ascending, the folder should be grouped at the at the bottom of the list.

 

Second, (besides the type sort) I'd have to loop through, build the array, sort the array then loop through that array to print the out put. I don't think this is the fastest way.

 

Can any one help?

Thanks

I had used filezilla in the past. I'm using XAMPP which includes filezilla. But the scripts I'm working are one that I'll be using for a CMS system later on. The idea on that was I could upload a bunch of files via FTP to a storage folder then use a script similar to the one I'm for help on to process and add to the database.

 

Plus this is a good php challenge and this a some thing new to learn.

Second, (besides the type sort) I'd have to loop through, build the array, sort the array then loop through that array to print the out put. I don't think this is the fastest way.

 

I can't think of a faster way. You've described what has to be done one way or another: loop through files in directory while building an array, sort that array, loop over sorted array for output.

For each file or folder I have created an array - $FileInfo[FileType],[FileName],[FileSize],[FileLocation]

This array stores all the File Information I need that I'd normally process during the fetch file loop.

 

This does work but I need to know how to sort this. I'm looking for sort method that will let me sort by;

[FileType],[FileName],[FileSize]

 

Kind of like sort($FileInfo[FileSize]) but with out the errors.

 

Here is the code I've come up with so far:

<?php
$Location = ".";
if( $Handle = opendir($Location) ) {
	$FoCount = 1; $FiCount = 1;

	$FolderArray[0] = "NULL DUMMY";
	$FileArray[0] = "NULL DUMMY";

	while( ($FFName = readdir($Handle)) !== false ) {
		if( $FFName !== "." && $FFName !== ".." && $FFName !== "Thumbs.db") {
			if( is_dir($Location."/".$FFName) ) {
				/* IGNORE FOR NOW - MORE FILES THAN FOLDERS ** \\
				$FolderArray[$FoCount] = $FFName ." - Folder";
				$FoCount += 1;
				*/
			} else {
				//$FileInfo[FileType],[FileName],[FileSize],[FileLocation];

				$FileExt = array_pop(explode(".", $FFName));
				$FileType = (  $FileExt == $FFName ) ? "Unknown" : $FileExt;

				$FileInfo[FileType] = $FileType;
				$FileInfo[FileName] = $FFName;
				$FileInfo[FileSize] = filesize($Location."/".$FFName);
				$FileInfo[FileLocation] = $Location."/".$FFName;

				$FileArray[$FiCount] = $FileInfo;
				$FiCount += 1;
			}
		}
	}
}
closedir($Handle);

// I need a way to sort by [FileType],[FileName],[FileSize]
sort($FileArray);

foreach( $FileArray as $aFile ) {
	if( $aFile !== "NULL DUMMY" ) {
		echo "{$aFile[FileName]} - {$aFile[FileType]} - {$aFile[FileSize]} - {$aFile[FileLocation]}<br>\n";
	}
}
?>

 

Thanks

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.