Code solutions to PHP, JS, CSSCode source PHP, JS, CSS

Spread your code soluces to the world

Love me
Solucior » PHP » Simple method : listing files from a directory

Simple method : listing files from a directory

05/19/2011 by Dux   -   not rated

Here's my simple method for listing files from a directory. In this example I added the natural sort and the file sizes in KB to show how to combine tables and serialized values.

source code



 
<?php
// Simple method
$dir = opendir('my_folder');
while (
$file = readdir($dir)) if ($file{0} != '.') echo $file.'<br />';
closedir($dir);


// Same method with sort and size
$folder = 'my_folder/';
$elm = array();
$dir = opendir($folder);
while (
$file = readdir($dir)) if ($file{0} != '.') $elm[] = $file .'|'.round(filesize($folder.$file)/1024,2);
closedir($dir);
natcasesort($elm);

foreach (
$elm as $disp) echo strtok($disp, '|').' ('.strtok('|').'KB)<br />';
?>
Rate this code     |     Copied : 20x

Comments for Simple method : listing files from a directory

No comment
Add a comment Nickname
ZOOM


Simple method : listing files from a directory
Simple image gallery PHP
Javascript opacity
Dynamic image resizing according to location
Zoom with CSS3
Make dir and upload files via FTP
Dynamically add rows to a form. Complete solution
Convert unknown text to ISO8859-1
Create virtual folders using htaccess
Scroll div with javascript