工作需要写了一个读取指定目录下的文件,并显示列表,点击之后读取文件中的内容

高手拍砖,目录可以自由指定,我这里直接写的是获取当前文件目录下面的所有文件

<?php

/**
* 读取指定目录下面的文件内容
* @author Administrator
*
*/
class Catlog { /**
* 要读取的目录
* @var string
*/
private $dir; /**
* 文件名中必须存在
* @var string
*/
private $str = 'ping'; public function __construct() {
$this->dir = getcwd();
} public function test() {
echo $this->dir;
} /**
* 获取指定目录下面的所有文件
* @return array
*/
public function getFile() { $dirArr = scandir($this->dir);
$fileInfo = array();
foreach( $dirArr as $k => $v ) {
if( !is_dir($v) && strpos($v, $this->str) !== FALSE ) { $filePath = $this->dir . '/' . $v; $fileArr['ctime'] = date('Y-m-d', filectime($filePath) );
$fileArr['mtime'] = date('Y-m-d', filemtime($filePath) );
$fileArr['atime'] = date('Y-m-d', fileatime($filePath) );
$fileArr['fileName'] = $v; $fileInfo[] = $fileArr;
}
} return $fileInfo;
} /**
* 获取某个文件的内容
* @return multitype:number string
*/
public function getFileContent() { if( isset($_GET['file_name']) && !empty($_GET['file_name']) ) {
$fileName = $_GET['file_name'];
$fileFullPath = $this->dir . '/' . $fileName; if( !is_file($fileFullPath) && !file_exists($fileFullPath) ) {
return $msg = array('error'=>1, 'msg'=>'文件不存在');
} else {
$content = file_get_contents($fileFullPath);
return $msg = array('error'=>0, 'msg'=>nl2br( $content) );
} } else {
return $msg = array('error'=>1, 'msg'=>'文件不存在');
} }//end catFileContent } $cat = new Catlog(); $notic = $cat->getFileContent(); if( $notic['error'] == 0 ) {
echo $notic['msg'];
} else { //显示网页内容 ?> <!DOCTYPE html>
<html>
<head>
<style type="text/css">
.time{ display:inline-block;
margin-right:200px;
float:right;
}
ol li { width:1000px; }
.file-name{ width:260px; display:inline-block; overflow:hidden;
white-space:nowrap;
-moz-text-overflow:ellipsis;
text-overflow:ellipsis; }
</style>
</head>
<body>
<ol>
<?php foreach($cat->getFile() as $k => $v ):?>
<li>
<a class="file-name" href="<?php echo '/catlog.php?file_name=' . $v['fileName'];?>"><?php echo $v['fileName']?></a>
<span class="time"><?php echo "创建时间:" . $v['ctime'] . "&nbsp;&nbsp;修改时间: " .$v['mtime'] . "&nbsp;&nbsp;上次阅读时间: " .$v['atime']?></span>
</li>
<?php endforeach;?>
</ol>
</body>
</html> <?php
}////显示网页内容 END
?>

php读取目录下的文件的更多相关文章

  1. C++ 递归读取目录下所有文件

    windows版本 #include <iostream> #include <io.h> #include <fstream> #include <stri ...

  2. python 读取目录下的文件

    参考方法: import os path = r'C:\Users\Administrator\Desktop\file' for filename in os.listdir(path): prin ...

  3. .net core 读取本地指定目录下的文件

    项目需求 asp.net core 读取log目录下的.log文件,.log文件的内容如下: xxx.log ------------------------------------------beg ...

  4. Java遍历目录下全部文件并替换指定字符串

    应用场景:比方有一个深层次的文件目录结构,如:javaAPI 每一个文件中面都有同样的内容,而我们要统一改动为其它内容.上千个文件假设一个个改动显得太不明智. import java.io.Buffe ...

  5. iOS案例:读取指定目录下的文件列表

    // // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...

  6. Python 读取某个目录下的文件

    读取某个目录下的文件,如'/Users/test/test_kmls'目录下有test1.txt.test2.txt. 第一种方法读出的all_files是test1.txt.test2.txt im ...

  7. [Java] 在 jar 文件中读取 resources 目录下的文件

    注意两点: 1. 将资源目录添加到 build path,确保该目录下的文件被拷贝到 jar 文件中. 2. jar 内部的东西,可以当作 stream 来读取,但不应该当作 file 来读取. 例子 ...

  8. AssetsUtils【读取assets、res/raw、./data/data/包名/目录下的文件】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装了以下功能: 1.读取assets目录下的资源html.文件.图片,将文件复制到SD卡目录中: 2.读取res/raw目录下的文 ...

  9. SpringBoot读取资源目录下的文件

    需要读取resources目录下的文件,那么方法如下: 假设在资源目录下的template目录下有一个文件a.txt,获取到文件流的方式 InputStream stream = this.getCl ...

随机推荐

  1. 编程异常——假设你报createSQLQuery is not valid without active transaction,...

    非常多时候我们使用hibernate的session时,都是让session在某一执行环境中保持其唯一. 比如在同一线程内用同一个session.在同一方法内用同一session,这样我们就能够用se ...

  2. Oracle数据库备份与恢复的常用方法

    Oracle数据库有三种常用的备份方法,分别是导出/导入(EXP/IMP).热备份和冷备份.导出/导入备份是一种逻辑备份,相对于导出/导入来说,热备份.冷备份是一种物理备份. 导出/导入(Export ...

  3. Linux文件时间属性

    Linux文件时间属性                                                                                         ...

  4. static使用方法小结

    static使用方法小结 statickeyword是C, C++中都存在的keyword, 它主要有三种使用方式, 当中前两种仅仅指在C语言中使用, 第三种在C++中使用(C,C++中详细细微操作不 ...

  5. Topcomponent --sighoff

    Topcomponent --sighoff 实现一个置于右边框的Topcomponent: 1.可远程同步更新(根据远程的xml文件),修改(增删)该xml文件,查看.刷新等 2.打包工程,记录该打 ...

  6. Flex-Security权限控制框架

    转自:http://code.google.com/p/flex-security/ flex UI组件权限控制框架 一.快速开始 1) 下载并添加flex_security.swf在你的flex l ...

  7. Linux服务器集群系统(二)--转

    引用地址:http://www.linuxvirtualserver.org/zh/lvs2.html LVS集群的体系结构 章文嵩 (wensong@linux-vs.org) 2002 年 4 月 ...

  8. CSS 之 控制图片与文字对齐

    文字旁边搭配图片时,发现图片比文字靠上,原来默认的情况是图片顶对齐而文字底对齐,通过设置css属性可以使得图片与文字对齐. 设置各对象的vertical-align属性,属性说明: baseline- ...

  9. Design Mode 之 创建模式

    A.创建模式 首先,简单工厂模式不属于24种涉及模式. A0.简单工厂模式 简单工厂模式,分为三种:普通简单工厂.多方法简单工厂.静态方法简单工厂. A01.普通 就是建立一个工厂类,对实现了同一接口 ...

  10. Cows

    Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can ...