duanguo7021 2011-10-26 15:10
浏览 140
已采纳

PHP readdir点不断显示

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

  • 写回答

2条回答 默认 最新

  • douyu9012 2011-10-26 15:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀