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

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

<?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. QML学习笔记之三

    import QtQuick 1.1 Row{ spacing:2 Rectangle{color:"red";width:50;height:50} Rectangle{colo ...

  2. Codeforces Gym 100015B Ball Painting 找规律

    Ball Painting 题目连接: http://codeforces.com/gym/100015/attachments Description There are 2N white ball ...

  3. Codeforces Good Bye 2015 A. New Year and Days 水题

    A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...

  4. DebugView 调试工具

    软件标签: DebugView调试工具 用debugview,打开debugview,运行你的debug版本程序,可以定位到源文件的某一行.在vc源码中需要输出的地方用 OutputDebugStri ...

  5. Android应用增量更新

    Original:https://github.com/cundong/SmartAppUpdates Backup:https://github.com/eltld/SmartAppUpdates

  6. windows 8.1 在硬盘上创建扩展分区

    管理员-命令提示符-> diskpart    -->进入分区工具. list disk     -->显示电脑连接磁盘数. select disk *  -->选择编号为*的 ...

  7. [STAThread]的含义

    Posted on 2007-07-07 10:06 桦林 阅读(33100) 评论(10) 编辑 收藏 [STAThread]STAThread:Single     Thread     Apar ...

  8. MapReduce原理及其主要实现平台分析

    原文:http://www.infotech.ac.cn/article/2012/1003-3513-28-2-60.html MapReduce原理及其主要实现平台分析 亢丽芸, 王效岳, 白如江 ...

  9. 基于HTML5的捕鱼达人游戏网页版

    之前给大家分享了html5实现的水果忍者,愤怒的小鸟,中国象棋游戏.今天给大家分享一款捕鱼达人(fishjoy)网页版游戏的源码.可以在线玩也可以下载到本地.它使用html5技术和javascript ...

  10. IOS 开发过程中的 消息通知 小红点

    大致分为两种方法:系统方法和自定义方法 系统方法: 系统自带的方法可以显示具体的消息数量,这个就是苹果设备常见的小红点.实现思路如下: NSArray *tabBarItems = self.navi ...