php专区

 首页 > php专区 > PHP高级 > 文件上传 > php 读目录以列表形式展示实例 - php文件操作

php 读目录以列表形式展示实例 - php文件操作

分享到:
【字体:
导读:
          本文章来给大家介绍利用php 读目录以列表形式展示,读取目录我们会使用到scandir,opendir,foreach,sizeof这几个常用的函数,下面我们直接看实例 例1,代码如下:$getUrl=(emptyempty($_GET[ 39;url...

php 读目录以列表形式展示实例

本文章来给大家介绍利用php 读目录以列表形式展示,读取目录我们会使用到scandir,opendir,foreach,sizeof这几个常用的函数,下面我们直接看实例.

例1,代码如下:

  1. $getUrl = (emptyempty($_GET['url'])) ? './' : $_GET['url'].'/';  
  2. function fileName($dir)  
  3. {  
  4.    
  5.     $fileAll = scandir($dir,0);  
  6.     $pathDir = $pathFile = array();  
  7.     $count = count($fileAll);  
  8.    
  9.     if($count <= 2){  
  10.         echo "空目录";  
  11.     }  
  12.        
  13.     foreach($fileAll as $pdf){  
  14.         if(is_dir($dir.$pdf)){  
  15.             $pathDir[] = $pdf;  
  16.         }else{  
  17.             $pathFile[] = $pdf;  
  18.         }  
  19.     }  
  20.    
  21.     foreach($pathDir as $pd){  
  22.         if($pd == '.' or $pd == '..'continue;  
  23.         echo "?url=$dir$pd">$pd";  
  24.     }  
  25.    
  26.     foreach($pathFile as $pf){  
  27.         echo "$dir$pf" target="_blank">$pf";  
  28.     }  
  29.    
  30.     if($dir != './'){  
  31.         $dir = rtrim($dir,'/');   
  32.         $dir = explode('/',$dir);  
  33.         unset($dir[sizeof($dir)-1]);  
  34.         $dir = implode('/',$dir);  
  35.         echo "?url=$dir">Go Back";  
  36.     }  
  37.    
  38. }  
  39.    
  40. fileName($getUrl); 

例2,代码如下:

  1.  
  2. /** 
  3.  * Goofy 2011-11-30 
  4.  * getDir()去文件夹列表,getFile()去对应文件夹下面的文件列表,二者的区别在于判断有没有“.”后缀的文件,其他都一样 
  5.  */ 
  6.  
  7. //获取文件目录列表,该方法返回数组 
  8. function getDir($dir) { 
  9.  $dirArray[]=NULL; 
  10.  if (false != ($handle = opendir ( $dir ))) { 
  11.   $i=0; 
  12.   while ( false !== ($file = readdir ( $handle )) ) { 
  13.    //去掉"“.”、“..”以及带“.xxx”后缀的文件 
  14.    if ($file != "." && $file != ".."&&!strpos($file,".")) { 
  15.     $dirArray[$i]=$file
  16.     $i++; 
  17.    } 
  18.   } 
  19.   //关闭句柄 
  20.   closedir ( $handle ); 
  21.  } 
  22.  return $dirArray
  23.  
  24. //获取文件列表 
  25. function getFile($dir) { 
  26.  $fileArray[]=NULL; 
  27.  if (false != ($handle = opendir ( $dir ))) { 
  28.   $i=0; 
  29.   while ( false !== ($file = readdir ( $handle )) ) { 
  30.    //去掉"“.”、“..”以及带“.xxx”后缀的文件 
  31.    if ($file != "." && $file != ".."&&strpos($file,".")) { 
  32.     $fileArray[$i]="./imageroot/current/".$file
  33.     if($i==100){ 
  34.      break
  35.     } 
  36.     $i++; 
  37.    } 
  38.   } 
  39.   //关闭句柄 
  40.   closedir ( $handle ); 
  41.  } 
  42.  return $fileArray
  43.  
  44. //调用方法getDir("./dir")……可以是绝对路径也可以是相对路径 
  45. ?> 
分享到:
PHP判断远程文件是否存在 - php文件操作
PHP判断远程文件是否存在 我们要处理的是判断一个远程服务器文件是否是存在,而不是自己本同服务器目录了,如果判断远程服务器的文件上否存在我们需要使用到curl_init,fsockopen,fopen方法,下面我来给大家举例说明. fopen函数 fopen() 函数打开文件或者 URL,如果打开失败,本函数返回 FALSE. ...
PHP删除指定文件夹根目录指定日期之前文...
PHP删除指定文件夹根目录指定日期之前文件 在php中删除目录中的文件我们需要遍历目录中的文件,然后获取文件的时间,我们再进行判断这个文件是不是过期了,如果适合条件就可以删除了. 删除文件,代码如下: file_delete = "home/meeta/my.php";  if (unlink($file_delete)) {  ec...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……