You had typos and errors:
-
Code:
while ($ff = readdir($dir))
if (is_image($f))
$imgs[] = IMG_DIR . '/' . $f;
$f does'nt exist, you mean $ff instead.
- You define IMG_DIR with 'images/' string, so this part makes it erroneous:
Code:
$imgs[] = IMG_DIR . '/' . $f;
...it will become images//
- You need to place the variables inside the PHP code:
Code:
<a href="$img">
<img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">
The rectified version is:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
define('IMG_DIR', 'images');
function is_image($fn) {
return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
}
$imgs = array();
$dir = opendir(IMG_DIR);
while ($ff = readdir($dir))
if (is_image($ff))
$imgs[] = IMG_DIR . '/' . $ff;
closedir($dir);
?>
<?php foreach ($imgs as $img)
{
echo '<a href="'.$img.'">
<img src="'.$img.'" alt="'.pathinfo($img, PATHINFO_FILENAME).'">
</a>';
}
?>
</body>
</html>
Hope that helps.
Bookmarks