Jump to content


Photo

Reading files and inserting it into an array?


  • Please log in to reply
6 replies to this topic

#1 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 26 June 2006 - 08:04 AM

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.



#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 26 June 2006 - 08:08 AM

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.
Think you're smarty?

(Gone until 20 to November)

#3 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 26 June 2006 - 10:45 AM

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...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#4 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 26 June 2006 - 12:30 PM

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 LITE?


#5 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 26 June 2006 - 03:28 PM

My pleasure.

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

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#6 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 26 June 2006 - 03:42 PM

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.

#7 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 26 June 2006 - 03:50 PM

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:
List files and directories inside the specified path.
Returns an array of files and directories from the directory.



And if you have PHP<5, use this:
<?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;
       }
   }
}
?>


Orio.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users