Jump to content

[SOLVED] Scan Dir


imdead

Recommended Posts

Hey Peoples :P,

I Made This For My Image Gallery

<?php
$thumbs = scandir('image');

foreach($thumbs as $thumb)
{
    if(substr($thumb, 0, 1) !== '.')
        echo '<img src="image/', $thumb, '"/ width="200" height="200">';
	echo '<a href="image/', $thumb, '"/>View Normal Size</a>';
}

?>

 

And It's Displaying It How It Should Execpt I Get Two Extra Links At The Front That Link To The Image Dir And One Links To The Main Root.

 

What Can You Guys Do? :P

Link to comment
https://forums.phpfreaks.com/topic/111551-solved-scan-dir/
Share on other sites

Don't Worry I Fixed Them Links But Now,

<?php
$thumbs = scandir('image');

foreach($thumbs as $thumb)
{
     if ($thumb[0]=='.') continue;
     if ($thumb[0]=='index.html') continue;
        echo '<img src="image/', $thumb, '"/ width="200" height="200">';
	echo '<a href="image/', $thumb, '"/>View Normal Size</a>';
}

?>

 

I Can't Make It Skip The Index.html file

Link to comment
https://forums.phpfreaks.com/topic/111551-solved-scan-dir/#findComment-572569
Share on other sites

<?php
$file_types = array('gif', 'jpg');

foreach(scandir('image') as $thumb)
{
    if (is_file($thumb) && in_array(end(explode('.', $thumb)), $file_types)) {
        echo '<img src="image/', $thumb, '"/ width="200" height="200">';
        echo '<a href="image/', $thumb, '"/>View Normal Size</a>';
    }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/111551-solved-scan-dir/#findComment-572606
Share on other sites

There was some errors in the echo code.

 

I fixed that and made it more modular. Try this:

 

<?php

$file_types = array('gif','jpg');
$directory = 'image';

foreach(scandir($directory) as $thumb)
{
    if (is_file("$directory/$thumb") && in_array(end(explode('.', $thumb)), $file_types))
    {
        echo "<img src=\"$directory/$thumb\" width=\"200\" height=\"200\">";
        echo "<a href=\"$directory/$thumb\">View Normal Size</a>";
    }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/111551-solved-scan-dir/#findComment-572620
Share on other sites

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.