PDA

View Full Version : PHP Reading folders and files



andrthad
January 23rd, 2004, 10:24 AM
Hi All,

Disclaimer: PHP newbie

I'm using a similar piece of code to read all the files in a certain web server directory into a long variable text string. Then I use loadvars to import into Flash and read into an array.



/* Returns the filenames from the directory */
<?
$dir = opendir("flash_1");
$i = 1;
while($file = readdir($dir))
if(is_file($file))
echo "&file". $i . "=" . $file;
?>


I understand the code above, but don't know the objects/functions to use to read folder names in PHP. Instead of reading file names, I would like to read all of the folder names inside a folder. For example, how would I write a PHP loop to return all the artist name folders inside a folder called music?

music
artist folder names (nested inside music folder)

Jubba
January 23rd, 2004, 11:10 AM
Its not actually that hard...


<?

$dir = "blog";

$handle = opendir($dir);

while(false!==($file = readdir($handle))){
if(is_dir($dir . "/" . $file) && $file != "." && $file != ".."){
// Do this with the file name.....
}
}

closedir($handle);

?>

hamza84
January 23rd, 2004, 11:14 AM
hey vash, what are the dots in the file!="." and file != ".." for? i know its related to directories, but what do they represent

Jubba
January 23rd, 2004, 11:20 AM
They take you back. They represent the folders one step and two steps back...

Say your files are set up like this:

monkey / horse / elephant / human

and your file is in the human directory

one period "." would take you to the elephant directory

and two periods ".." would take you to the horse directory

The reason you will usually see them in an if statement is because we usually don't care about them. They usually aren't part of the output we want. and in this case he seems to want only the artist names....

kill.robot.kill
January 23rd, 2004, 11:35 AM
you see them sometimes in certain FTP programs also. But the main problem is them getting listed when you want to list all the files in a folder....usually something like an image folder.

hamza84
January 23rd, 2004, 09:42 PM
if i wanted to access the monkey folder, would i put three dots then? and by the way, i never intended to say 'what the heck' in my previous post, just slipped by somehow :(. Being inapropriate (and it was), I've edited it now. Anyway, I can;t understand this part of the code:

if(is_dir($dir . "/" . $file) .....

the $file part is not making sense to me. Appreciate it if you could elaborate a little itsy bitsy bit on this :D

Jubba
January 23rd, 2004, 10:51 PM
i would try something like

./../ to get back to the monkey folder... I'm not sure if 3 would work like that...

well $file is everything that is being read by the function...

and $dir is our main dir... so we have

$dir . "/" . $file really means blog/$file

then the is_dir() is just checking to see if what is being read is a directory or a file...

does that help? or does it sound like gibberish? I'm really sick right now so nothing may make any sense...

hamza84
January 24th, 2004, 12:44 AM
that does help. just wondering what the slash is for. Get Well soon!

norie
January 24th, 2004, 02:12 PM
spend a little time in a command line ftp or OS (unix, linux, dos) you'll get really good at relative folder referencing ;)

hamza84
January 24th, 2004, 03:21 PM
an example for starters plz?

andrthad
January 26th, 2004, 01:38 PM
Thanks Vash. Actually I want to drill down like so.

music / artist names / albums / .mp3

I am going to try this php script to see what it outputs. I may be back with more questions.