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 ...
随机推荐
- 实战项目:通过当当API将订单抓取到SAP(二)
上一篇博客,我们引用了log4net 这个.这里简单介绍下,为什么引用这个. log4net是记录程序日志信息的,是一个功能著名的开源日志记录组件.利用log4net可以方便地将日志信息记录到文件.控 ...
- Codeforces Round #180 (Div. 2) C. Parity Game 数学
C. Parity Game 题目连接: http://www.codeforces.com/contest/298/problem/C Description You are fishing wit ...
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...
- cdoj 04 Complete Building the Houses 暴力
Complete Building the Houses Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/# ...
- Out of resources when opening file 错误解决
mysqldump: Got error: 23: Out of resources when opening file ‘./mydb/tax_calculation_rate_title.MYD’ ...
- C#中listbox中选中多项,并删除
1.SelectionMode 改成可以多选2.利用KeyDown事件: private void listBox1_KeyDown(object sender, KeyEventArgs e) { ...
- LeetCode16 3Sum Closest
题意: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 开发工具 之 PowerDesigner 应用积累
1.在默认情况下,code与name是联动,修改了name中的数据. 解决方法:设置菜单栏选择"Tools→General Options→Dialog" 中的 "Na ...
- 1.7.2 Velocity Search UI
1. Velocity Search UI solr包含了一个简单的搜索UI界面,是基于VelocityResponseWrite(也被叫做 Solritas)的.这里演示了几个有用的功能,如搜索,分 ...
- uiview 的transitionWithView 方法使用
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [UIView transitionWithView:_imageV ...