好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php中打开目录并输出目录文件实现代码 - php文件

php中打开目录并输出目录文件实现代码

opendir() 函数打开一个目录句柄,可由 closedir(),readdir() 和 rewinddir() 使用.

语法: opendir(path,context),实例代码如下:

  • */ 
  • $d =dir( "c:windows" );          //打开目录,并赋值给变量  
  • echo   "handle:" . $d ->handle. "n" ;        //输出目录的handel属性  
  • echo   "<p>"
  • echo   "path:" . $d ->path. "n" ;         //输出目录的path属性  
  • echo   "<p>"
  • while (false!==( $entry = $d ->read()))        //通过循环读取对象的方法  
  •   echo   $entry . "n"
  • $d ->close();            //关闭目录句柄  
  •  
  •  
  • //  
  •  
  •  
  • $dir = "c:windows" ;           //定义目录  
  • if ( is_dir ( $dir ))            //如果变量为目录  
  •   if ( $dh =opendir( $dir ));          //如果成功打开目录  
  •   { 
  •     echo   "目录" . $dir . "成功打开"
  •   } 
  • echo   "<br>"
  • closedir ( $dh );           //关闭打开的目录句柄  
  • echo   "目录" . $dir . "已经关闭!"
  •  
  •  
  • ///  
  •  
  • $dir = " c:windows " ;           //定义目录变量  
  • if ( is_dir ( $dir ))            //判断变量是否为目录  
  •   if ( $dh =opendir( $dir ))          //判断目录是否成功打开  
  •   { 
  •     while (( $file =readdir( $dh ))!==false)       //循环读取目录中的文件信息  
  •     { 
  •       echo   "filename:$file,filetype:" . filetype ( $dir . $file ). "n" ;   //输出文件名及文件类型  
  •     } 
  •     closedir ( $dh );           //关闭目录句柄  
  •   } 
  •  
  • ///  
  •  
  • if ( $handle =opendir( 'web' ))          //如果目录顺利打开  
  •   while (false!==( $file =readdir( $handle )))       //循环读取目录中的条目  
  •   { 
  •     if ( $file != "." && $file != ".." )         //如果文件不为当前或上级目录  
  •     { 
  •       echo   "$filen" ;           //输出文件名称  
  •     } 
  •   } 
  •   closedir ( $handle );           //关闭目录句柄  
  •  
  • //  
  •  
  • if ( $handle =opendir( '08' ))          //如果目录顺利打开  
  •   while (false!==( $file =readdir( $handle )))       //循环读取目录中的条目  
  •   { 
  •     if ( $file != "." && $file != ".." )         //如果文件不为当前或上级目录  
  •     { 
  •       echo   "$filen" ;           //输出文件名称  
  •     } 
  •   } 
  •   rewind ( $handle );           //倒回目录句柄  
  •   if (readdir( $handle ))        //判断是否倒回成功  
  •   { 
  •     echo   "倒回成功"
  •   } 
  •   else  
  •   { 
  •     echo   "倒回失败"
  •   } 
  •   closedir ( $handle );   //关闭目录句柄  
  •  
  • //如果要对目录按字母排序如一  
  • //开源代码phpfensi.com  
  • $files1 =scandir( $dir );    //按字母升序列出内容到数组  
  • $files2 =scandir( $dir ,1);    //按字母降序列出内容到数组

    查看更多关于php中打开目录并输出目录文件实现代码 - php文件的详细内容...

  •   阅读:67次