找回密码
 注册
首页 ≡≡网络技术≡≡ PHP语言 php自动读取文件夹下所有图片并分页显示 ...

PHP php自动读取文件夹下所有图片并分页显示

灰儿 2016-10-27 15:41:45
实现目标:
原因是我有大量的图片需要上传,不想在数据库中添加或者在cms网站后台一个一个的添加上去,我想把图片文件夹通过ftp上传到网站根目录内,想在图片文件夹下放置一个index.php文件,在网页中打开该图片文件夹时,自动加载该图片文件夹里面的所有图片,并分页显示出来,这代码如何实现?

推荐实现代码1(有分页显示功能,点击缩略图打开原图):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php自动读取文件夹下所有图片并分页显示_1</title>
</head>
<body bgcolor=000000>
<center>
  <font size=2 color=red>
  <?php  
$page=$_GET['page'];//获取当前页数  
  
$max=5;//设置每页显示图片最大张数  
  
$path=".";     //图片文件夹路径,如果图片放在当前目录用“.”表示
  
$handle = opendir($path); //当前目录  
  
    while (false !== ($file = readdir($handle))) { //遍历该php文件所在目录  
  
      list($filesname,$kzm)=explode(".",$file);//获取扩展名  
  
        if($kzm=="gif" or $kzm=="jpg" or $kzm=="JPG") { //文件过滤  
  
          if (!is_dir('./'.$file)) { //文件夹过滤  
  
            $array[]=$file;//把符合条件的文件名存入数组  
  
            $i++;//记录图片总张数  
  
           }  
  
          }  
  
    }  
  
for ($j=$max*$page;$j<($max*$page+$max)&&$j<$i;++$j){//循环条件控制显示图片张数  
  
    //echo "<img widht=800 height=600 src=\".$path"\".$array[$j].">";//输出图片数组  
  
echo "<a href=".$path."/".$array[$j]."><img width=150 height=110 src=".$path."/".$array[$j]."></a><br>";  
  
}  
  
$Previous_page=$page-1;  
  
$next_page=$page+1;  
  
if ($Previous_page<0){  
  
    echo "上页";  
  
    echo "<a href=?page=$next_page>下页</a>";  
  
}  
  
    else if ($page<=$i/$max){  
  
      echo "<a href=?page=$Previous_page>上页</a>";  
  
      echo "<a href=?page=$next_page>下页</a>";}  
  
        else{  
  
          echo " <a href=?page=$Previous_page>上页</a>";  
  
          echo "下页";  
  
        }  

?>
</center>
</body>
</html>

来源地地址:http://blog.csdn.net/youngqj/article/details/7091938

推荐实现代码2(有分页显示功能,有返回首页链接):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php自动读取文件夹下所有图片并分页显示_2</title>
</head>

<body>
<div style="width: 90%; margin: 10px auto; border: 1px solid #ccc; text-align: center">
  <?php
    $page=isset($_GET['page'])?$_GET['page']:0;//从零开始
    $imgnums = 5;    //每页显示的图片数
    $path=".";   //图片保存的目录,如果图片放在当前目录用"."表示
    $handle = opendir($path);
    $i=0;
    while (false !== ($file = readdir($handle))) {
       list($filesname,$ext)=explode(".",$file);
       if($ext=="gif" or $ext=="jpg" or $ext=="JPG" or $ext=="GIF" ) {
           if (!is_dir('./'.$file)) {
              $array[]=$file;//保存图片名称
              ++$i;
           }
       }
    }
    if($array){
       rsort($array);//修改日期倒序排序
    }
    for($j=$imgnums*$page; $j<($imgnums*$page+$imgnums)&&$j<$i; ++$j){
       echo '<div>';
       echo $array[$j],'<br />';
       echo "<img src=".$path."/".$array[$j]."><br />";
       echo '</div>';
    }
    $realpage = @ceil($i / $imgnums) - 1;
    $Prepage = $page-1;
    $Nextpage = $page+1;
    if($Prepage<0){
       echo "上一页 ";
       echo "<a href=?page=$Nextpage>下一页</a> ";
       echo "<a href=?page=$realpage>最末页</a> ";
    }elseif($Nextpage >= $realpage){
       echo "<a href=?page=0>首页</a> ";
       echo " <a href=?page=$Prepage>上一页</a> ";
       echo " 下一页";
    }else{
       echo "<a href=?page=0>首页</a> ";
       echo "<a href=?page=$Prepage>上一页</a> ";
       echo "<a href=?page=$Nextpage>下一页</a> ";
       echo "<a href=?page=$realpage>最末页</a> ";
    }
    ?>
</div>
</body>
</html>



来源网址:http://www.jb51.net/article/47971.htm

另一种代码(没有分页显示功能):

  1. <?php
  2.         $dir = "./images/";  //要获取的目录
  3.         echo "********** 获取目录下所有文件和文件夹 ***********<hr/>";
  4.         //先判断指定的路径是不是一个文件夹
  5.         if (is_dir($dir)){
  6.                 if ($dh = opendir($dir)){
  7.                         while (($file = readdir($dh))!= false){
  8.                                 //文件名的全路径 包含文件名
  9.                                 $filePath = $dir.$file;
  10.                                 echo "<img src='".$filePath."'/>";
  11.                         }
  12.                         closedir($dh);
  13.                 }
  14.         }
  15. ?>
复制代码
或者:
  1. $dir = './images/';
  2. if (is_dir($dir)) {   
  3.     if ($dh = opendir($dir)) {   
  4.         while (($file = readdir($dh)) !== false) {   
  5.             if ($file!="." && $file!="..") {   
  6.                     echo '<img src="'.$dir.'/'.$file.'"/>';
  7.             }   
  8.         }   
  9.         closedir($dh);   
  10.     }   
  11. }  
复制代码


灰儿 楼主 2016-10-27 15:54:56
phpFORAlbum

http://www.pudn.com/downloads530 ... /detail2194949.html

详细说明:这个PHP脚本能够自动获取指定目录下的所有图片,然后生成一个可按文件日期或文件名排序并进行分组的相册。


经测试没有效果,并且需要数据库支持才能用。
灰儿 楼主 2017-1-4 16:45:24
PHP如何获取文件夹下所有子文件夹的名称及子文件夹所有图片

scandir函数可以列车目录下面的文件和目录
opendir 打开目录
is_dir() 判断是否是目录
is_dir()判断是否为文件
这个用到的技术就php对文件的操作,文件遍历。
这里有个我自己写文件夹遍历函数,你看看测试下。

  1. //递归遍历文件夹,输出所有文件的路径
  2. function bianli($path){
  3.     static $arr=array();
  4.     $str="";
  5.     if($open=opendir($path)){
  6.         while($f=readdir($open)){
  7.             $str=$path;
  8.             if($f!='.' && $f!='..'){
  9.                 $str=$str."/".$f;
  10.                 if(is_dir($str)){
  11.                     $i++;
  12.                     bianli($str);  
  13.                 }else{
  14.                     $arr[]=$str;
  15.                     //echo $str."";
  16.                 }         
  17.             }         
  18.         }
  19.     }
  20.      
  21.     return $arr;
  22. }

  23. //遍历文件夹
  24. function search_one_file($path){
  25.     $arr=array();
  26.     if($open=opendir($path)){
  27.         while($f=readdir($open)){
  28.             if($f!='.' && $f!='..'){
  29.                 $arr[]=$path."/".$f;
  30.             }
  31.         }
  32.     }
  33.     return $arr;
  34. }
复制代码

灰儿 楼主 2017-1-5 11:23:01
php遍历目录下所有文件代码(此例只取图片)

$path = '/home/ictspace/www/img/avatar';
function getfiles($path){
   if(!is_dir($path)) return;
   $handle  = opendir($path);
   $files = array();
   while(false !== ($file = readdir($handle))){         
    if($file != '.' && $file!='..'){
     $path2= $path.'/'.$file;
     if(is_dir($path2)){
     getfiles($path2);         
     }else{
        if(preg_match("/\.(gif|jpeg|jpg|png|bmp)$/i", $file)){
        $files[] = $path.'/'.$file;
     }
     }         
    }
   }
   return $files;
}


灰儿 楼主 2017-1-5 11:26:11
php遍历目录输出目录及其下的所有文件示例

function my_scandir($dir){
$files=array();
if(is_dir($dir)){
if($handle=opendir($dir)){
while(($file=readdir($handle))!==false){
if($file!='.' && $file!=".."){
if(is_dir($dir."/".$file)){
$files[$file]=my_scandir($dir."/".$file);
}else{
$files[]=$dir."/".$file;
}
}
}
}
}
closedir($handle);
return $files;
}
echo my_scandir($dir);


注:经测试可用


灰儿 楼主 2017-1-5 11:30:44
function file_list($path){

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

    if ($handle = opendir($path)) {
   
        while (false !== ($file = readdir($handle))) {
        
            if ($file != "." && $file != "..") {
            
                if (is_dir($path."/".$file)) {
                 
                    //echo $path.": ".$file."<br>";//去掉此行显示的是所有的非目录文件
                  file_list($path."/".$file);
                } else {
                    
if(strpos($file,'.png',1)||strpos($file,'.jpg',1)||strpos($file,'.ico',1)||strpos($file,'.gif',1)){
echo '<img src="'.$path.'/'.$file.'" />';
    }
                }
            }
        }
    }
}
echo file_list($path);


注:经测试可用,能显示所在文件夹和子件夹下的图片

在PHP中 if ($file != "." && $file != "..")是什么意思

如果变量$file不是.并且不是..执行后面语句
应该用来判断目录的
灰儿 楼主 2017-1-5 20:17:06
php删除指定文件/获取文件夹的所有文件名   

一、删除文件。

unlink()
语法: int unlink(string filename);
返回值: 整数
函数种类: 文件存取。如:unlink("tmp/test.txt");

二、获取文件夹下面的文件名

$dir = "message/";  // 文件夹的名称
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "文件名: $file <br>";
}
closedir($dh);
}
}
三、读取文件夹下面的图片名,这个代码很好用。

<?php
/*获取指定文件夹中的指定扩展名的所有文件,返回数组**/
function get_files_by_ext($path, $ext){

$files = array();

if (is_dir($path)){
$handle = opendir($path);
while ($file = readdir($handle)) {
if ($file[0] == '.'){ continue; }
if (is_file($path.$file) and preg_match('/\.'.$ext.'$/', $file)){
$files[] = $file;
}
}
closedir($handle);
sort($files);
}

return $files;

}

/*
** 获取文件夹的名字,并返回数组
*/
function getDir($dir) {
$dirArray[]=NULL;
if (false != ($handle = opendir ( $dir ))) {
$i=0;
while ( false !== ($file = readdir ( $handle )) ) {
//去掉"“.”、“..”以及带“.xxx”后缀的文件
if ($file != "." && $file != ".."&&!strpos($file,".")) {
$dirArray[$i]=$file;
$i++;
}
}
//关闭句柄
closedir ( $handle );
}
return $dirArray;
}


/*获取指定文件夹中的所有文件,并返回数组*/
function getFile($dir) {
$fileArray[]=NULL;
if (false != ($handle = opendir ( $dir ))) {
$i=0;
while ( false !== ($file = readdir ( $handle )) ) {
//去掉"“.”、“..”以及带“.xxx”后缀的文件
if ($file != "." && $file != ".."&&strpos($file,"."))
/*($file != "." && $file != ".."&&strpos($file,"."))*/ {
echo $file;
$fileArray[$i]=$file;
if($i==100){
break;
}
$i++;
}
}
//关闭句柄
closedir ( $handle );
}
return $fileArray;
}


print_r(getDir('d:/php100/'));
echo'<br />';
print_r(get_files_by_ext('d:/php100/', 'pdf'));

?>
您需要登录后才可以回帖 登录 | 注册
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。