PHP readdir 点不断显示

标签 php html css lightbox

我写了一个简单的图片库脚本女巫检查文件夹并显示文件夹中的所有图像我有代码删除 .和 .. 但它不起作用,我不明白为什么任何帮助都会很棒,因为我是 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/

相关文章:

javascript - 有没有办法将图像异步加载到用户的缓存中?

html - Bootstrap 最大 2 列 div 彼此相邻

html - 显示 :table & display:table-cell not working in IE9

html - 当div达到窗口宽度时如何添加css规则

css - 悬停文本时淡化图像

javascript - 无法通过 PHP 函数将 JSON 剩余响应获取到数组中

php - 使用 CURL 保存图像,有时会保存空白图像

javascript - JSON 数据到 HTML

JavaScript 函数更改属性值

javascript - 使用 Javascript 访问 MyBB PHP 变量