+ -
当前位置:首页 → 问答吧 → 遍历目录得到图片并输出

遍历目录得到图片并输出

时间:2007-08-26

来源:互联网

//整理:dx_andy
//测试环境:win32 apache2.2.4 php5.2.1 mysql5.0.27

<?php

$imgtype=array('bmp','jpg','jpeg','png','gif');   //初始化图片文件扩展名

$imgtype_count=count($imgtype);                //计算共有多少图片扩展名

$path=".";        //设定目录

$handle=opendir($path);                //打开目录

while ($file = readdir($handle))    //取得目录中的文件名
{

if (is_dir($file)) {continue;}                //如果$file为目录,则不做操作

$type = explode(".",$file);                        //分割字符串

$type=$type[1];                //得到文件扩展名

for($i=0;$i<$imgtype_count;$i++)
{
if($type==$imgtype[$i])                //判断文件扩展名是否为图片文件的扩展名,若是则做下列输出
        {
        echo"<a href=".$path."/".$file." target=\"_blank\" alt=\"点击打开新窗口浏览\"><img src=".$path."/".$file." border=\"0\" onload=\"if(this.height>150) {this.height=150;this.width=150*this.width/this.height;}\"></a>\n";
        }
}

}
closedir($handle);                //关闭目录

?>

作者: dx_andy   发布时间: 2007-08-26

:victory:

作者: forest   发布时间: 2007-08-26

很好.思路很不错

作者: 逆雪寒   发布时间: 2007-08-26

作者: hejingke   发布时间: 2007-12-28

思路不错。

作者: thomas   发布时间: 2007-12-29

如果$file为目录,则不做操作?
写一个函数并进行递归调用,极简单的事,何不试一下?

看看这一句:
$type = $type[1];

// $type = strtolower($type[count($type)-1]);
谁能保证文件名是标准的"name.ext"的格式?
谁能保证文件名是全小写的?

作者: sztime   发布时间: 2007-12-29

可以改进为查找(搜索)指定文件存在否。

作者: zdchjj   发布时间: 2008-01-13

...........

且不说这样获得的文件类型是否正确,单从
复制PHP内容到剪贴板
PHP代码:
for($i=0;$i<$imgtype_count;$i++)

if($type==$imgtype[$i])                //判断文件扩展名是否为图片文件的扩展名,若是则做下列输出
        {
        echo"<a href=".$path."/".$file." target=\"_blank\" alt=\"点击打开新窗口浏览\"><img src=".$path."/".$file." border=\"0\" onload=\"if(this.height>150) {this.height=150;this.width=150*this.width/this.height;}\"></a>\n";
        } 
}

}

这块来讲,为什么不用 in_array()来做判断?

作者: 无喱头   发布时间: 2008-01-14

<?php
function OutputPic($path)
{
        if($path=="")
                $path="./";        //$path为空时,设定为当前目录
        $imgtype=array('bmp','jpg','jpeg','png','gif');   //初始化图片文件扩展名
        $imgtype_count=count($imgtype);                //计算共有多少图片扩展名
       
        $handle=opendir($path);                //打开目录
        while($file = readdir($handle))    //取得目录中的文件名
        {
                if(is_dir($file)){continue;}         //如果$file为目录,则不做操作
                $type = explode(".",$file);                //分割字符串                    
                $type=end($type);                //得到文件扩展名
                $type2 = strtolower($type);        把文件扩展名都转为小写,再判断
                if(in_array($type2,$imgtype))   //判断文件扩展名是否为图片文件的扩展名,若是则做下列输出
                {
                        echo"<a href=".$path."/".$file." target=\"_blank\" alt=\"点击打开新窗口浏览\"><img src=".$path."/".$file." border=\"0\" onload=\"if(this.height>150) {this.height=150;this.width=150*this.width/this.height;}\"></a>";
                }
        }
        closedir($handle);      //关闭目录
}

$imgpath1 = "./image";
$imgpath2 = "";

echo OutputPic($imgpath1);
?>


顶LZ,楼上几位,在提出问题的同时,是否也可以写点代码改进下呢?偶新手,相信大部分新人也希望这样

作者: sundyandy   发布时间: 2008-01-18