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

A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/

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');
?>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>"; } ?>
Code: Select all
<?php
$str1 = "FileNameExample.Extention";
$str3 = explode('.', $str1 , 2 );
echo $str3[1]
?>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);
?>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>";
}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>";
}
?>