Jump to content

Reading files and inserting it into an array?


bilis_money

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.

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

Archived

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

×
×
  • Create New...

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.