|
-
Jun 7th, 2002, 04:23 PM
#1
Thread Starter
Stuck in the 80s
All da Files 'n Stuff, man
How can I get all the files in a given directory? I'm bored and want to write a file manager in PHP.
-
Jun 7th, 2002, 04:24 PM
#2
Thread Starter
Stuck in the 80s
I know it probably has something to do with the readdir() function, but I'm not sure how to use it?
-
Jun 7th, 2002, 04:25 PM
#3
Thread Starter
Stuck in the 80s
Nevermind, got it: 
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);
}
?>
-
Jun 7th, 2002, 08:16 PM
#4
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)) {
PHP 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.";
?>
but I am glad I could help
-
Jun 7th, 2002, 08:30 PM
#5
Thread Starter
Stuck in the 80s
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);
}
?>
-
Jun 7th, 2002, 08:32 PM
#6
Thread Starter
Stuck in the 80s
Also:
"$a !== $a" - Not identical: TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
-
Jun 7th, 2002, 08:39 PM
#7
-
Jun 7th, 2002, 09:06 PM
#8
Thread Starter
Stuck in the 80s
haha. I haven't had a chance to test this code. Do you think it'll get directories too, or just files?
-
Jun 7th, 2002, 09:16 PM
#9
Fanatic Member
it will get both.
You will need to use is_file() or is_dir() to distinguish between the two.
-
Jun 7th, 2002, 09:17 PM
#10
Thread Starter
Stuck in the 80s
-
Jun 7th, 2002, 09:21 PM
#11
Fanatic Member
Originally posted by The Hobo
Thanks, Brother Mark!
Who is Mark??
-
Jun 7th, 2002, 09:22 PM
#12
Thread Starter
Stuck in the 80s
I don't know
-
Jun 7th, 2002, 11:02 PM
#13
Originally posted by cpradio
it will get both.
You will need to use is_file() or is_dir() to distinguish between the two.
if it does anything like the one in my book, which is almost the exact code, it will list the extensions as well.
-
Jun 7th, 2002, 11:19 PM
#14
Thread Starter
Stuck in the 80s
Is there a function that can get the file type, such as "HTML Document" "Image" "PHP Document" ??
-
Jun 7th, 2002, 11:19 PM
#15
Fanatic Member
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
-
Jun 7th, 2002, 11:30 PM
#16
Originally posted by The Hobo
Is there a function that can get the file type, such as "HTML Document" "Image" "PHP Document" ??
I don't think so. you will have to make one, or if you had a friend he could give you one
-
Jun 8th, 2002, 08:21 AM
#17
Fanatic Member
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
-
Jun 8th, 2002, 09:09 AM
#18
Thread Starter
Stuck in the 80s
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
|