Viewing Image from directory HELP

images php coding issues or problems here.

Moderators: macek, egami, gesf

kumel619
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Jul 25, 2011 12:13 am

Viewing Image from directory HELP

Postby kumel619 » Mon Jul 25, 2011 12:14 am

I have a page whr pictures r been displayed from the directory..
So when imgs r dr in directory it gets displayed in a page thru php code... Problem is whn dr s no image in dat directory den i get long error.. it irritates..

Codes are below-

<?php
$dirname = "directory-name/";

$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src='directory-name/$curimg' /><br/>
<br/>
\n";

};
}
?>


Please help me in displaying CUSTOM ERROR MSG IF NO images found in dat directory.

NigelRen
php-forum Active User
php-forum Active User
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: Viewing Image from directory HELP

Postby NigelRen » Fri Aug 05, 2011 10:18 am

There are a couple of things you need to do ( I think )
First is to remove the / from the end of the directory name, I think this will allow it to pick up the directory properly. The second thing is to trap if the scandir has failed. Sacndir will return false if it hasn't worked for any reason so something like...

Code: Select all

<?php
$dirname = "directory-name";

$images = scandir($dirname);
if ( $images ) {
    $ignore = Array(".", "..");
    foreach($images as $curimg){
        if(!in_array($curimg, $ignore)) {
            echo "<img src='directory-name/$curimg' /><br/>
            <br/>
            \n";
        }
    }   
}
?>


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest

cron