How can I get all the files in a given directory? I'm bored and want to write a file manager in PHP.
Printable View
How can I get all the files in a given directory? I'm bored and want to write a file manager in PHP.
I know it probably has something to do with the readdir() function, but I'm not sure how to use it?
Nevermind, got it: :rolleyes:
PHP Code:<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
closedir($handle);
}
?>
are you sure that works?
while (false !== ($file = readdir($handle))) {
look all like errors. what is false in there for? and !== is not right.
while ($file = readdir($handle)) {
but I am glad I could help :pPHP Code:<?php
if (!$handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
}else{
echo "Cannot open $default_dir.";
?>
Scoutt...I'm disappointed. Straight from the Manual:
PHP Code:// Note that !== did not exist until 4.0.0-RC2
<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
}
?>
Also:
Quote:
"$a !== $a" - Not identical: TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
whoa there cowboy :p
I didn't say it was totally wrong. My book has something a little different. I never looked at the manual. I know !== is valid code but why check for identical files if you don't have anything to compare it too.
edit: well never mind my mindless babbleing. I looked at the manual and found out that my book is waaaaayyyyyyyyy out of date. time to buy a new one. it must be pre php4.0 :)
carry on :D
haha. I haven't had a chance to test this code. Do you think it'll get directories too, or just files?
it will get both.
You will need to use is_file() or is_dir() to distinguish between the two.
Thanks, Brother Mark!
Who is Mark?? :confused:Quote:
Originally posted by The Hobo
Thanks, Brother Mark!
I don't know :confused:
if it does anything like the one in my book, which is almost the exact code, it will list the extensions as well.Quote:
Originally posted by cpradio
it will get both.
You will need to use is_file() or is_dir() to distinguish between the two.
Is there a function that can get the file type, such as "HTML Document" "Image" "PHP Document" ??
yes that is true, when I said "distinguish" i meant that if he wanted to list all directories and sort them in alphabetical order ontop of all the file he would have to be able to figure out which ones are directories and which are files based on those php functions.
-Matt
I don't think so. you will have to make one, or if you had a friend he could give you one :)Quote:
Originally posted by The Hobo
Is there a function that can get the file type, such as "HTML Document" "Image" "PHP Document" ??
Ya you will have to make your own. the filetype() function only returns one of the following: "fifo","char","dir","block","link","file","unknown"
What I would personally do is grab the extension of the file and run it across an array or table I have built determining what kind of document it is.
-Matt
Gotcha. Thanks.