Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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