Jump to content


Photo

reading certain files in directory


  • Please log in to reply
3 replies to this topic

#1 j0se

j0se
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 26 April 2006 - 04:49 PM

anyone know where i can find a way to read a directory and only read like jpg files or txt files in the directory?

#2 Orio

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

Posted 26 April 2006 - 06:37 PM

You can use the "scandir($dir)" fucntion to get an array with all the files and diretories in the directory, and then run a loop.
Here:
$filetype=(the type of file you need);
$files=scandir($dir);
$num=count($files);
$i=0;
while($i<$num)
{
if(is_file($files[$i])){
$var=pathinfo($files[$i]);
if($var['extension']==$filetype){
echo($files[$i]);
};//close second if
};//close first if
};//close while
This echos all the files with the extension you set.

Orio.


**EDIT**
Just found this:
[a href=\"http://www.php.net/manual/en/function.scandir.php\" target=\"_blank\"]http://www.php.net/manual/en/function.scandir.php[/a]
Check the seond from top user note by www.mdsjack.bo.it
Think you're smarty?

(Gone until 20 to November)

#3 Kris

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

Posted 26 April 2006 - 06:44 PM

A possible alternative could use glob():
<?php
$ext = array("txt","jpg");
$files = array();
foreach($ext as $e) {
    foreach(glob("*.$e") as $filename) {
        $files[] .= $filename;
    }
}
?>
$files contains all of the .txt and .jpg filenames as an array.

#4 j0se

j0se
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 26 April 2006 - 07:12 PM

thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users