Jump to content

Archived

This topic is now archived and is closed to further replies.

bilis_money

Reading files and inserting it into an array?

Recommended Posts

hi & hello PHP addicts...

hi, i want to read all the files inside a directory and put them in an array.
let say i have /photos/ directory and i want to get the files that reside in there and insert them
into array.
can you show me the way you do this? your style?
I have already an idea with this but i'm just curious to know yours?


Thanks in advance.

Share this post


Link to post
Share on other sites
Requires PHP5 [a href=\"http://www.php.net/manual/en/function.scandir.php\" target=\"_blank\"]scandir()[/a]
If you have PHP<5, read the user posts below, and you'll find a function for that.

Orio.

Share this post


Link to post
Share on other sites
Look at the script on this page, it has a scetion that reads the contents of a folder into an array. works with several versions of php.


[a href=\"http://www.nstoia.com/mygallery/\" target=\"_blank\"]http://www.nstoia.com/mygallery/[/a]

Lite...

Share this post


Link to post
Share on other sites
Thank you very much -->[!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--] litebearer[!--sizec--][/span][!--/sizec--].
Thanks for the link, and by the way is that your personal webiste [b]LITE[/b]?

Share this post


Link to post
Share on other sites
My pleasure.

Yes, my personal site, which is in a state of constant flux as I learn how to do more things.

Lite...

Share this post


Link to post
Share on other sites
the function [a href=\"http://www.php.net/glob\" target=\"_blank\"]glob()[/a] returnsan array of all of the files/directories within a specified directory.

Share this post


Link to post
Share on other sites
I still think that [a href=\"http://www.php.net/manual/en/function.scandir.php\" target=\"_blank\"]scandir()[/a] is the best solution for this one.

It's defenition is:
[i]List files and directories inside the specified path.
Returns an array of files and directories from the directory.[/i]


And if you have PHP<5, use this:
[code]<?php
if(!function_exists('scandir')) {
   function scandir($dir, $sortorder = 0) {
       if(is_dir($dir))        {
           $dirlist = opendir($dir);
           while( ($file = readdir($dirlist)) !== false) {
               if(!is_dir($file)) {
                   $files[] = $file;
               }
           }
           ($sortorder == 0) ? asort($files) : rsort($files);
           return $files;
       } else {
       return FALSE;
       break;
       }
   }
}
?>[/code]


Orio.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.