Forum Moderators: coopster

Message Too Old, No Replies

Directory structure into an array

How to create an array of a directory structure

         

PumpkinHead

11:54 am on Jul 11, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm trying to find out how to get PHP to create an array of a given directory's structure.

E.g

Structure Array =

1 - Blue Images
2 - Red Images
3 - Pink Images
4 - Brown Images

The above strucutre will be changing daily, so I need this to be dynamic. Any ideas how to do this? I don't really know what to search for!

Thanks in advance if anyone can help.

dreamcatcher

12:25 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

To read from a directory, try opendir(), readdir() & closedir(). That should get you started.

[uk.php.net...]

Example:


$open = opendir("files/");

while ($read = readdir($open))
{
if ($read!= "." && $read!= "..")
{
echo $read . '<br>';
}
}
closedir($open);

Hope that gets you started.

dc

PumpkinHead

1:12 pm on Jul 11, 2005 (gmt 0)

10+ Year Member



Many thanks dreamcatcher, that's a great start. I have the array working now :)

Wondering...is it possible to get the file types?

i.e

1 - Blue Images (Folder)
2 - Red Images (Folder)
3 - Pink Images (Folder)
4 - Brown Images (Folder)
5 - image1.jpg (File)
6 - image2.jpg (File)

Thanks again

gliff

1:47 pm on Jul 11, 2005 (gmt 0)

10+ Year Member



You can use the is_dir [us2.php.net] function to check if a path points to a directory or a file.

dreamcatcher

2:51 pm on Jul 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PumpkinHead,

I don`t think its actually possible to get the file types from reading the file, although I could be wrong. Some of the more experienced guys will know. ;)

What you can do is get the file extension from the file and build data from there. To get the file extension use the strrchr() [uk.php.net] function. So, for example first build an array of file types:

$types = array(
'JPeg Image File',
'Gif Image File'
);

Then when you loop your code do something like:

$open = opendir("files/");

while ($read = readdir($open))
{
if ($read!= "." && $read!= "..")
{

echo '(' . $read . ') File Type: ';

switch (strrchr(strtolower($read), '.'))
{
case ".jpg":
echo $types[0];
break;

case ".gif":
echo $types[1];
break;
}
echo '<br>';

}
}
closedir($open);

Something like that would work. Add as many types to the array and switch as you need. Just make sure they match. I`ve also used strtolower to eliminate any case issues. I`m sure there is something simpler, but its hot and sunny in the UK and thats all I can think of right now. LOL.

dc

coopster

11:28 am on Jul 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



dc, I think PumpkinHead meant the difference between a folder and a file (see msg#3), and gliff offered up a good solution (filetype [php.net] is another option).

If you want to find the MIME type of the file you can do a number of things, one of which you have already shown, read the extension. Another option is mime_content_type [php.net]. If memory serves me correctly, the Image functions also have a couple of MIME-type functions that prove useful.

dreamcatcher

2:31 pm on Jul 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for clearing that up Coop. :)