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

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

<?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. [每日一题] OCP1z0-047 :2013-08-06 外表部――相关描述

    这道题目的知识点是要你熟悉外部表,怎么建外部表,外部表的数据是怎么存储的等等.请给出正确答案,并解释A B C D每项,最好用实验测试证明! 外部表的metadata(元数据)是存在数据库中,但它的数 ...

  2. LFS7.4编译笔记(3)

    在第一部分,我们编译了一个工具链及临时系统,然后在第二部分我们chroot到/mnt/lfs下面,利用临时系统的工具编译了我们最终的LFS系统.不过此时,我们的LFS系统还是不完整的,因为我们还没有安 ...

  3. iOS 原生地图(MapKit、MKMapView)轨迹渐变

    WechatIMG2.png 项目已接入高德地图,并且大部分功能已经实现好,但BOSS觉得iOS自带的地图效果更好...本着面向老板编程的思想,换之.还好,高德地图是在MapKit上封装的,大部分ap ...

  4. crm2011创建货币Money类型的字段

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Messages;     using Microsoft ...

  5. Foundation框架之NSString及其Mutable类型

    Foundation框架之NSString及其Mutable类型 目录 概述 对字符串的实用操作 拼接 拆分 字符串比较 是否包含某字符串 字数统计 大小写转换 具体的方法参见API 待研究 概述 对 ...

  6. 深入理解 CSS 中的行高与基线

    1.基本概念 1.  基线.底线.顶线.中线 注意:基线(base line)并不是汉字文字的下端沿,而是英文字母“x”的下端沿. 2. 内容区 内容区是指底线和顶线包裹的区域(行内元素display ...

  7. [转]Oracle快速入门

    原文出处:http://blog.csdn.net/yueguanghaidao/article/details/7019377 select * from scott.salgrade; /*解锁s ...

  8. C#获取进程的主窗口句柄的实现方法

    通过调用Win32 API实现. public class User32API { private static Hashtable processWnd = null; public delegat ...

  9. C. Om Nom and Candies 巧妙优化枚举,将复杂度控制在10e6

    C. Om Nom and Candies 无线超大背包问题 #include <iostream> #include <cstdio> #include <cstrin ...

  10. 【python,logging】python中的logging模块

    本文章转自kenby的博客,比较全面易懂,转来留作以后使用. http://kenby.iteye.com/blog/1162698 一.从一个使用场景开始 import logging # 创建一个 ...