PHP readdir 不断显示点

发布于 2024-12-12 02:09:50 字数 1627 浏览 0 评论 0原文

我编写了一个简单的图像库脚本,女巫检查文件夹并显示文件夹中的所有图像,我有代码删除 .并且..但它不起作用,我不明白为什么任何帮助都会很棒,因为我对 PHP 中的 dir 函数很陌生。我在链接周围有灯箱标签,因此有额外的回声代码如下:

<?
// 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); 
}

?>

提前感谢

刘易斯

I have written a simple image gallery scrip witch checks a folder and displays all the images in the folder i have code to remove the . and .. but its not working and I cant see why any help would be great as im new to the dir functions in PHP. I have lightbox tags surrounding the link hence the extra echo's The code is below:

<?
// 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); 
}

?>

Thanks in Advance

Lewis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2024-12-19 02:09:50

if ($file!="."&&$file!="..") 后面没有大括号,if 语句仅适用于第一行 echo "

";

将所有内容都用大括号括起来,如下所示:

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 来进一步简化代码。

You have no braces after if ($file!="."&&$file!=".."), the if statement is only applicable for the first line of echo "<div class='imgwraper'>";.

Wrap everything in braces like so:

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

By the way, you could consider using glob to simplify your code a bit more.

情绪失控 2024-12-19 02:09:50

不要忘记将 if 语句括在大括号中。

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

谢伊。

Don't forget to wrap your if statement in curly brackets.

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

Shai.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文