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

Answers


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

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.


Need Your Help

I don't know why my webpage won't stop loading

google-chrome debugging web google-chrome-devtools loading

I'm still a newbie when it comes to backend stuff and I have a web page that won't stop loading. How would I even troubleshoot this? I've gone into the Networking tab of Inspect Element but I'm not...

Connect to MS Access remote .mdb file from php on linux

php ms-access odbc jet unixodbc

I have been digging internet for couple days, reading very old information, that leads to very old and nonexisting sites, still, I understood, what is needed to achieve my goal.