我写了一个简单的图片库脚本女巫检查文件夹并显示文件夹中的所有图像我有代码删除 .和 .. 但它不起作用,我不明白为什么任何帮助都会很棒,因为我是 PHP 中 dir 函数的新手。我在链接周围有灯箱标签,因此额外的 echo 代码如下:
<?
// USER OPTIONS DEDFINED HERE
$dir = "img/"; //folder with images
$height ="196px"; //image height on page its displayed in full in lightbox
$width ="320px"; //image width on page its displayed in full in lightbox
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Gallery</title>
<link href="css/css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="js/lightbox.js"></script>
</head>
<?
//open directory
if ($opendir = opendir($dir));
{
//read dir
while (FALSE !== ($file = readdir($opendir)))
{
if ($file!="."&&$file!="..")
echo "<div class='imgwraper'>";
echo"<a href='$dir$file' rel='lightbox[gallery]' title='$filename'>";
echo "<img src='$dir$file' alt='' width='$width' height='$height'/>";
echo"</a>";
echo "<div class='name_box'>";
echo current(explode('.', $file));
echo "</div>";
echo "</div>";
}
closedir($opendir);
}
?>
提前致谢
刘易斯
最佳答案
if ($file!="."&&$file!="..")
之后你没有戴牙套, if
语句仅适用于 echo "<div class='imgwraper'>";
的第一行.
像这样用大括号包裹所有内容:
while (FALSE !== ($file = readdir($opendir)))
{
if ($file!="."&&$file!="..") {
echo "<div class='imgwraper'>";
echo"<a href='$dir$file' rel='lightbox[gallery]' title='$filename'>";
echo "<img src='$dir$file' alt='' width='$width' height='$height'/>";
echo"</a>";
echo "<div class='name_box'>";
echo current(explode('.', $file));
echo "</div>";
echo "</div>";
}
}
顺便说一下,你可以考虑使用 glob
进一步简化您的代码。
关于PHP readdir 点不断显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7904742/