Array range with scandir

Hello,

I want to display pictures from a specific folder, but only some of them, the order doesn’t matter as long as they are not repeated.

This works fine, except it shows everything.

 <?php $dirs = "azure/arts";

$pics = scandir($dirs);

foreach ($pics as $item) { if (($item != ".") && ($item != "..")) {

echo '<a target="_blank" href="' . $dirs . $item . '"><img src="' . $dirs . $item . '" alt="' . $item . '" title="' . $item . '" class="drawgala" /></a> '; } } ?> 

I tried this, but it didn’t work

 <?php $dirs = "azure/arts";

$pics = scandir($dirs);

foreach ($pics(1, 50) as $item) { if (($item != ".") && ($item != "..")) {

echo '<a target="_blank" href="' . $dirs . $item . '"><img src="' . $dirs . $item . '" alt="' . $item . '" title="' . $item . '" class="drawgala" /></a> '; } } ?> 

thanks :slight_smile:

How are you going to determine what images are to be shown??

I have a feeling the answer to your question is going to be “use [FPHP]glob/FPHP instead”, but like iamjones, i’m curious how you’re actually filtering.

It looks like you are wanting to use something like array_slice() to get a limited number of files from the full array.


$chosen_pics = array_slice($pics, 0, 50);
foreach ($chosen_pics as $item) {

Bear in mind that this will include directories (even the . and .. ones), so it’s also a good idea to filter the array. One way to get only the files from the array would be to use array_filter() with [url=http://php.net/is_file]is_file().


$pics = array_filter('is_file', $pics);
$chosen_pics = array_slice($pics, 0, 50);
foreach ($chosen_pics as $item) {

thank you Salathe! that was exactly what I was looking for. :slight_smile: