Search code examples
phpfilepathinfo

php pathinfo problem


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";
}

Solution

  • 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";
    }