Jump to content

[SOLVED] Creating an array from files in a directory


cory011202

Recommended Posts

Hello all,

 

I am fairly new to php and I am trying to create an array from files listed in a directory such as images.

Below is my code so far. I would like to take the results and put them into an array that I can display on a page. In the end I will have the images change every hour from top to bottom (that is the plan atleast). Any help or direction anyone can give would be greatly appreciated. I hope I have posted this in the correct forum topic.

Thanks in advance.

 

 

 

<?php

$open = opendir("./test/");

 

while ($read = readdir($open))

{

if ($read!= "." && $read!= "..")

{

echo $read . '<br>';

}

}

closedir($open);

 

?>

Try

 

<?php
$files = array();
$dir = '/test';
while($file = readdir($dir)){
     if($file != '.' and $files != '..'){
          $files[] = $file;
     }
}
closedir($dir);
print_r($files); //show the contents of $files array
?>

 

or alternatively you can use glob():

<?php
$files = array();
foreach(glob('/test/*.jpg') as $file){
     $files[] = $file;
}
print_r($files);
?>

 

Hope that helps and if you have anymore questions, feel free to ask.

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.