php读取目录下的文件
工作需要写了一个读取指定目录下的文件,并显示列表,点击之后读取文件中的内容
高手拍砖,目录可以自由指定,我这里直接写的是获取当前文件目录下面的所有文件
<?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'] . " 修改时间: " .$v['mtime'] . " 上次阅读时间: " .$v['atime']?></span>
</li>
<?php endforeach;?>
</ol>
</body>
</html> <?php
}////显示网页内容 END
?>
php读取目录下的文件的更多相关文章
- C++ 递归读取目录下所有文件
windows版本 #include <iostream> #include <io.h> #include <fstream> #include <stri ...
- python 读取目录下的文件
参考方法: import os path = r'C:\Users\Administrator\Desktop\file' for filename in os.listdir(path): prin ...
- .net core 读取本地指定目录下的文件
项目需求 asp.net core 读取log目录下的.log文件,.log文件的内容如下: xxx.log ------------------------------------------beg ...
- Java遍历目录下全部文件并替换指定字符串
应用场景:比方有一个深层次的文件目录结构,如:javaAPI 每一个文件中面都有同样的内容,而我们要统一改动为其它内容.上千个文件假设一个个改动显得太不明智. import java.io.Buffe ...
- iOS案例:读取指定目录下的文件列表
// // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...
- Python 读取某个目录下的文件
读取某个目录下的文件,如'/Users/test/test_kmls'目录下有test1.txt.test2.txt. 第一种方法读出的all_files是test1.txt.test2.txt im ...
- [Java] 在 jar 文件中读取 resources 目录下的文件
注意两点: 1. 将资源目录添加到 build path,确保该目录下的文件被拷贝到 jar 文件中. 2. jar 内部的东西,可以当作 stream 来读取,但不应该当作 file 来读取. 例子 ...
- AssetsUtils【读取assets、res/raw、./data/data/包名/目录下的文件】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装了以下功能: 1.读取assets目录下的资源html.文件.图片,将文件复制到SD卡目录中: 2.读取res/raw目录下的文 ...
- SpringBoot读取资源目录下的文件
需要读取resources目录下的文件,那么方法如下: 假设在资源目录下的template目录下有一个文件a.txt,获取到文件流的方式 InputStream stream = this.getCl ...
随机推荐
- hdu 4493 Tutor 水题
Tutor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4493 D ...
- Visual Studio原生开发的10个调试技巧(一)
最近碰巧读了Ivan Shcherbakov写的一篇文章,<11个强大的Visual Studio调试小技巧>.这篇文章只介绍了一些有关Visual Studio的基本调试技巧,但是还有其 ...
- 如何在XAF中显示自定义窗体和控件
https://www.devexpress.com/Support/Center/Example/Details/E911
- [Node.js] CommonJS Modules
CoomonJS modules provide a clean syntax for importing dependencies. This lesson will take a look at ...
- Android Widget 小部件(三) 在Activity中加入Widget
package com.stone.ui; import static android.util.Log.d; import android.app.Activity; import android. ...
- Java_基础_内存管理
把没几多年,完全忘记了把自己学的东西记录下来了,现在也基本不知道怎么去记录会更好了,不过好歹妹是把住了~也要毕业了,继续回来写东东记录自己的学习...... 一个Java程序在运行时的内存分布主要如上 ...
- mysql官方示例数据库
employees数据库:http://ari.iteye.com/blog/1066690 https://launchpad.net/test-db/employees-db-1/1.0.6
- 1.7.7 Spell Checking -拼写检查
1. SpellCheck SpellCheck组件设计的目的是基于其他,相似,terms来提供内联查询建议.这些建议的依据可以是solr字段中的terms,外部可以创建文本文件, 或者其实lucen ...
- Comparison method violates its general contract
生产环境出现的错误排查,错误log如下 java.lang.IllegalArgumentException: Comparison method violates its general contr ...
- ubuntu创建、删除文件及文件夹,强制清空回收站方法
mkdir 目录名 => 创建一个目录 rmdir 空目录名 => 删除一个空目录 rm 文件名 文件名 => 删除一个文件或多个文件 rm –rf 非 ...