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. Ho开发者_JAVA技巧w would i achieve this?
$path_info = pathinfo($dir.$file);
$extension = $path_info['extension'];
if($extension) {
echo "<img class=\"thumbnail\" src=\"".$dir.$file."\" />\n";
}
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
.
精彩评论