i want to know how can i show all the files in a directory? thanks
Printable View
i want to know how can i show all the files in a directory? thanks
I posted code similiar to this not too long ago:
Hope this helps.Code:<?php
if ($handle = opendir('/home/your_site/folder')) {
while (false !== ($file = readdir($handle))) {
echo "$file<br>\n";
}
closedir($handle);
}
?>
how do i make only show files?
Opps. Sorry about that. Just use the is_file() function to check to see if it's a file:
These functions and code snippets can be found in the manual at www.php.net It's an invaluable resource. :)Code:<?php
if ($handle = opendir('/home/your_site/folder')) {
while (false !== ($file = readdir($handle))) {
if (is_file($file)) {
echo "$file<br>\n";
}
}
closedir($handle);
}
?>
thanks i have this code
<?php
echo '<table>';
if ($handle = opendir('C:/Site/dks/')) {
while (false !== ($file = readdir($handle))) {
if (is_file($file)) {
echo '<tr><td>' . $file . '</td><td>FILESIZE</td></tr>';
}
}
closedir($handle);
}
echo '</table>';
?>
how do i filesize?
Did you not follow the link I gave you?Quote:
Originally posted by php_n00b
how do i filesize?
Here is a link o File System Functions and Directory Functions.
Check out the function called filesize().
This is how you'd do it:
Code:<?php
echo '<table>';
if ($handle = opendir('C:/Site/dks/')) {
while (false !== ($file = readdir($handle))) {
if (is_file($file)) {
echo '<tr><td>' . $file . '</td><td>' . formatsize(filesize($file)) . '</td></tr>';
}
}
closedir($handle);
}
echo '</table>';
function formatsize($fs) {
if($fs < 1024) {
return $fs . " B";
} else if($fs < pow(1024, 2)) {
return ceil($fs / 1024) . " KB";
} else {
return ceil($fs / pow(1024, 2)) . " MB";
}
}
?>
why not in alpha order???
You're probably going to have to load the file names into an array, then use the sort() function, then loop through and echo them out.
I'll post code in a minute...
This should work:
Code:<?php
//get the files:
if ($handle = opendir('C:/Site/dks/')) {
$i = 0;
while (false !== ($file = readdir($handle))) {
if (is_file($file)) {
//add file to array:
$files[$i][0] = $file;
$files[$i][1] = formatsize(filesize($file));
$i++;
}
}
closedir($handle);
}
//sort the array:
sort($files);
//output the files:
echo '<table>';
for ($i = 0; $i < count($files); $i++) {
echo '<tr><td>' . $files[$i][0] . '</td><td>' . $files[$i][1] . '</td></tr>';
}
echo '</table>';
//function to format file size:
function formatsize($fs) {
if($fs < 1024) {
return $fs . " B";
} else if($fs < pow(1024, 2)) {
return ceil($fs / 1024) . " KB";
} else {
return ceil($fs / pow(1024, 2)) . " MB";
}
}
?>
can you put the ones that were created today in pink twotwos :p
:rolleyes:
should have made him look for how to do it....
For that you'll have to load the fd_si library and use the _SetT() function.Quote:
Originally posted by phpman
can you put the ones that were created today in pink twotwos :p
I'll post code in a minute. :p
Another way:
Code:$dYre = 'your folder, like /uploads or /myDir';
$dh = opendir($dYre);
while (gettype($file = readdir($dh)) != 'boolean')
{
if (is_dir("$dYre/$file"))
print '<font color=777777><b>[Directory]</b> ';
if (is_file("$dYre/$file"))
print '<font color=FF0000><b>[File]</b> ';
print $file.'</font><br>';
}
closedir($dh);