tags:

views:

174

answers:

2

I have this snippet and there are 3 images and 3 folders in the directory. It echos the images just fine but it also gives me this error for each of the folders.

Notice: Undefined index: extension in D:\Data\Websites\wamp\www\StephsSite\PHP\manage.php on line 119

What i want to do is have it so if it finds a file with no extension(a folder) display an static image. How would i achieve this?

$path_info = pathinfo($dir.$file);
$extension = $path_info['extension'];

if($extension) {
    echo "<img class=\"thumbnail\" src=\"".$dir.$file."\" />\n";
}
A: 

You can use isset to check if the array returned by pathinfo has 'extension' as a key:

$path_info = pathinfo($dir.$file);

if(isset($path_info['extension'])) {
    echo "<img class=\"thumbnail\" src=\"".$dir.$file."\" />\n";
}

When a directory is passed to pathinfo, the array returned does not have 'extension' as a key and when you try to access it using $path_info['extension'] you get the

Undefined index Notice.

codaddict
Is there a function that specifically looks to see if something is a folder or not?
Catfish
is_dir http://php.net/manual/en/function.is-dir.php
Marius
+2  A: 

You can use array_key_exists to check if a key exists in the $path_info array

$path_info = pathinfo($dir.$file);

if(array_key_exists('extension', $path_info)) {
  $extension = $path_info['extension'];
  echo "<img class=\"thumbnail\" src=\"".$dir.$file."\" />\n";
}
Marius