PHP文件与目录操作示例


Posted in PHP onDecember 24, 2016

本文实例讲述了PHP文件与目录操作。分享给大家供大家参考,具体如下:

文件目录相关函数

<?php
// 输出目录中的文件
function outputcurfiles ($allowedtypes, $thedir){
//首先,我们确保目录存在。
if (is_dir ($thedir)){
 //现在,我们使用scandir扫描目录中的文件。
 $scanarray = scandir ($thedir);
 //接着我们开始解析数组。
 //scandir()用“.”和“..”统计文件导航列表
 //因此作为文件,我们不应该列出他们。
 for ($i = 0; $i < count ($scanarray); $i++){
  if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
   //现在,进行检查,以确保这是一个文件,而不是一个目录。
   if (is_file ($thedir . "/" . $scanarray[$i])){
    //现在,因为我们将允许客户端编辑这个文件,
    //我们必须检查它是否是可读和可写。
    if (is_writable ($thedir. "/" . $scanarray[$i]) &&  is_readable($thedir . "/" . $scanarray[$i])){
     //现在,我们检查文件类型是否存在于允许的类型数组中.
     $thepath = pathinfo ($thedir . "/" . $scanarray[$i]);
     if (in_array ($thepath['extension'], $allowedtypes)){
      //如果文件符合规定,我们可以继续输出.
      echo $scanarray[$i] . "<br />";
     }
    }
   }
  }
 }
} else {
 echo "对不起,这个目录不存在.";
}
}
$allowedtypes = array ("txt","html");
outputcurfiles ($allowedtypes, "testfolder");
///////////////////////////////////////////////////
function recurdir ($thedir) {
  //First attempt to open the directory.
  try {
    if ($adir = opendir ($thedir)){
      //扫描目录。
      while (false !== ($anitem = readdir ($adir))){
        //不统计目录中包含“.”或“..”的情况
        if ($anitem != "." && $anitem != ".."){
          //此时如果是一个目录,则缩进一点
          //再去递归
          if (is_dir ($thedir . "/" . $anitem)){
            ?><span style="font-weight: bold;" mce_style="font-weight: bold;"><?php echo $anitem; ?></span><?php
            ?><div style="margin-left: 10px;" mce_style="margin-left:10px;"><?php
            recurdir ($thedir . "/" . $anitem );
            ?></div><?php
          } elseif (is_file ($thedir . "/" . $anitem)){
            //此时输出文件.
            echo $anitem . "<br />";
          }
        }
      }
    } else {
      throw new exception ("Sorry, directory could not be openend.");
    }
  } catch (exception $e) {
    echo $e->getmessage();
  }
}
echo "<br />/////////////////////////////////////<br /><br />";
recurdir("testfolder");
//////////////////////////////////////////////////////////////////
echo "<br />/////////////////////////////////////<br /><br />";
function sortfilesbydate ($thedir){
  //首先,需要确保目录存在。
  if (is_dir ($thedir)){
    //接着,我们使用scandir扫描此目录中的文件.
    $scanarray = scandir ($thedir);
    $finalarray = array();
    //然后开始解析数组
    //scandir()用“.”和“..”统计文件导航列表
    //因此作为文件,我们不应该列出他们.
    for ($i = 0; $i < count ($scanarray); $i++){
      if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
        //现在,我们检查,以确保这是一个文件,而不是一个目录.
        if (is_file ($thedir . "/" . $scanarray[$i])){
          //现在需要做的是循环数据到一个关联数组.
          $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);
        }
      }
    }
    //至此,我们已经遍历了整个数组,现在需要做的只是asort()它。
    asort ($finalarray);
    return ($finalarray);
  } else {
    echo "对不起,这个目录不存在.";
  }
}
//然后,我们将函数指向我们需要查看的目录.
$sortedarray = sortfilesbydate ("testfolder");
//至此,就可以按照如下形式输出:
while ($element = each ($sortedarray)){
  echo "File: " . $element['key'] . " was last modified: " . date ("F j, Y h:i:s", $element['value']) . "<br />";
}
?>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
GBK的页面输出JSON格式的php函数
Feb 16 PHP
mysql,mysqli,PDO的各自不同介绍
Sep 19 PHP
一些php项目中比较通用的php自建函数的详解
Jun 06 PHP
PHP COOKIE及时生效的方法介绍
Feb 14 PHP
destoon官方标签大全
Jun 20 PHP
thinkphp多层MVC用法分析
Dec 30 PHP
详解EventDispatcher事件分发组件
Dec 25 PHP
php在windows环境下获得cpu内存实时使用率(推荐)
Feb 08 PHP
PHP与以太坊交互详解
Aug 24 PHP
PHP面向对象程序设计之多态性的应用示例
Dec 19 PHP
PHP基于curl实现模拟微信浏览器打开微信链接的方法示例
Feb 15 PHP
PHP日期和时间函数的使用示例详解
Aug 06 PHP
PHP数组操作实例分析【添加,删除,计算,反转,排序,查找等】
Dec 24 #PHP
PHP常见字符串处理函数用法示例【转换,转义,截取,比较,查找,反转,切割】
Dec 24 #PHP
PHP会话控制实例分析
Dec 24 #PHP
PHP面向对象程序设计方法实例详解
Dec 24 #PHP
PHP数据库处理封装类实例
Dec 24 #PHP
如何判断php mysqli扩展类是否开启
Dec 24 #PHP
Thinkphp框架中D方法与M方法的区别
Dec 23 #PHP
You might like
php设计模式 Decorator(装饰模式)
2011/06/26 PHP
简单实现限定phpmyadmin访问ip的方法
2013/03/05 PHP
解析PHP留言本模块主要功能的函数说明(代码可实现)
2013/06/25 PHP
php从数组中随机选择若干不重复元素的方法
2015/03/14 PHP
PHP MSSQL 分页实例
2016/04/13 PHP
PHP常见错误提示含义解释(实用!值得收藏)
2016/04/25 PHP
CI框架源码解读之URI.php中_fetch_uri_string()函数用法分析
2016/05/18 PHP
JQuery 应用 JQuery.groupTable.js
2010/12/15 Javascript
setTimeout函数兼容各主流浏览器运行执行效果实例
2013/06/13 Javascript
使用jsonp完美解决跨域问题
2014/11/27 Javascript
jQuery中delegate()方法用法实例
2015/01/19 Javascript
JavaScript中length属性的使用方法
2015/06/05 Javascript
JavaScript ES2019中的8个新特性详解
2019/02/20 Javascript
Nodejs + Websocket 指定发送及群聊的实现
2020/01/09 NodeJs
使用js获取身份证年龄的示例代码
2020/12/11 Javascript
Vue过滤器,生命周期函数和vue-resource简单介绍
2021/01/12 Vue.js
[04:56]经典回顾:前Ehome 与 前LGD
2015/02/26 DOTA
python 自动提交和抓取网页
2009/07/13 Python
Redis使用watch完成秒杀抢购功能的代码
2018/05/07 Python
利用Anaconda简单安装scrapy框架的方法
2018/06/13 Python
python机器学习包mlxtend的安装和配置详解
2019/08/21 Python
python os.path.isfile()因参数问题判断错误的解决
2019/11/29 Python
基于pytorch中的Sequential用法说明
2020/06/24 Python
CSS3 3D酷炫立方体变换动画的实现
2019/03/26 HTML / CSS
美国爆米花工厂:The Popcorn Factory
2019/09/14 全球购物
Blue Nile蓝色尼罗河香港官网:世界最大在线钻石珠宝销售商
2020/05/07 全球购物
UML设计模式笔试题
2014/06/07 面试题
大学生求职自荐信
2013/12/12 职场文书
募捐倡议书
2014/04/14 职场文书
交通事故调解协议书
2014/04/16 职场文书
男性健康日的活动方案
2014/08/18 职场文书
护士优质服务演讲稿
2014/08/26 职场文书
党校培训学习心得体会
2016/01/06 职场文书
人为什么会“幸灾乐祸”?
2019/08/06 职场文书
浅谈Python基础之列表那些事儿
2021/05/11 Python
HTML怎么设置下划线?html文字加下划线方法
2021/12/06 HTML / CSS