|
-
Mar 26th, 2003, 02:09 PM
#1
Thread Starter
New Member
show files
i want to know how can i show all the files in a directory? thanks
-
Mar 26th, 2003, 02:13 PM
#2
Stuck in the 80s
I posted code similiar to this not too long ago:
Code:
<?php
if ($handle = opendir('/home/your_site/folder')) {
while (false !== ($file = readdir($handle))) {
echo "$file<br>\n";
}
closedir($handle);
}
?>
Hope this helps.
-
Mar 26th, 2003, 02:20 PM
#3
Thread Starter
New Member
how do i make only show files?
-
Mar 26th, 2003, 02:23 PM
#4
Stuck in the 80s
Opps. Sorry about that. Just use the is_file() function to check to see if it's a file:
Code:
<?php
if ($handle = opendir('/home/your_site/folder')) {
while (false !== ($file = readdir($handle))) {
if (is_file($file)) {
echo "$file<br>\n";
}
}
closedir($handle);
}
?>
These functions and code snippets can be found in the manual at www.php.net It's an invaluable resource.
-
Mar 26th, 2003, 03:02 PM
#5
Thread Starter
New Member
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?
-
Mar 26th, 2003, 03:14 PM
#6
Stuck in the 80s
Originally posted by php_n00b
how do i filesize?
Did you not follow the link I gave you?
Here is a link o File System Functions and Directory Functions.
Check out the function called filesize().
-
Mar 26th, 2003, 03:23 PM
#7
Stuck in the 80s
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";
}
}
?>
-
Mar 26th, 2003, 04:04 PM
#8
Thread Starter
New Member
why not in alpha order???
-
Mar 26th, 2003, 04:06 PM
#9
Stuck in the 80s
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...
-
Mar 26th, 2003, 04:13 PM
#10
Stuck in the 80s
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";
}
}
?>
-
Apr 4th, 2003, 03:55 PM
#11
Frenzied Member
can you put the ones that were created today in pink twotwos 
should have made him look for how to do it....
-
Apr 4th, 2003, 04:00 PM
#12
Stuck in the 80s
Originally posted by phpman
can you put the ones that were created today in pink twotwos
For that you'll have to load the fd_si library and use the _SetT() function.
I'll post code in a minute.
-
Apr 29th, 2003, 08:20 PM
#13
Fanatic Member
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);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|