php文件操作技巧FileSystem
来源:互联网 发布:js 去除disabled 编辑:程序博客网 时间:2024/06/05 17:49
1.获取目前下的子目录(目录下的所有同级文件包括文件夹)
php代码
$path = '/data/logs';$dirChirdenList = scandir($path);var_dump($dirChirdenList);
输出
array(28) { [0] => string(1) "." [1] => string(2) ".." [2] => string(8) "activity" [3] => string(5) "admin" [4] => string(4) "auth" [5] => string(13) "codeexception" [6] => string(6) "coobar" [7] => string(16) "coobar_error_log" [8] => string(4) "cron" [9] => string(9) "http_api6" [10] => string(13) "http_service6" [11] => string(8) "http_sso" [12] => string(5) "nginx" [13] => string(3) "pay" .... ...}
php代码
$handler = opendir('/data/logs');$files = [];while( ($filename = readdir($handler)) !== false ) { $files[] = $filename;}closedir($handler);var_dump($files);
输出
array(28) { [0] => string(6) "wallet" [1] => string(13) "codeexception" [2] => string(19) "php-fpm54.error.log" [3] => string(15) "php54.error.log" [4] => string(17) "php-fpm.error.log" [5] => string(7) "upgrade" [6] => string(18) "php-fpm54.slow.log" [7] => string(4) "auth" [8] => string(8) "activity" [9] => string(16) "coobar_error_log" [10] => string(13) "http_service6" [11] => string(3) "sql" [12] => string(1) "." [13] => string(6) "rsyncd" ... ...}
递归获取该文件下所有文件
$dir = "/databackup/php18/logs/codeexception";$fileList = [];$result = getDirFile($dir,$fileList);function getDirFile($dir,&$fileList){ $handler = opendir($dir); while (($filename = readdir($handler)) !== false) { if ($filename == '.' || $filename == '..') { continue; } if (is_dir($dir . "/" . $filename)) { getDirFile($dir . "/" . $filename,$fileList); } else { $fileList[] = $dir . "/" . $filename; } } closedir($handler); return true;}
2.将日志文件变成一个数组 每一行一个key (不推荐打开大文件内容 容易内存溢出)
文件内容
row1row2row3row4row5row6
php代码
$rows = file('/home/hls/test/test.log);var_dump($rows);
输出
array(6) { [0] => string(5) "row1" [1] => string(5) "row2" [2] => string(5) "row3" [3] => string(5) "row4" [4] => string(5) "row5" [5] => string(5) "row6"}
3.逐行读取文件内容(适合大文件 文件分析)
文件内容
row1row2row3row4row5row6
php代码
$handler = fopen('/home/hls/test/test.log','r');$rows = [];while (!feof($handler)) { $row = fgets($handler); if ($row !== false) { $rows[] = $row; }}fclose($handler);dump($rows);exit;
输出
array(6) { [0] => string(5) "row1" [1] => string(5) "row2" [2] => string(5) "row3" [3] => string(5) "row4" [4] => string(5) "row5" [5] => string(5) "row6"}
阅读全文
0 0
- php文件操作技巧FileSystem
- PHP Filesystem 函数---文件操作函数
- hadoop中的文件操作 FileSystem
- boost-文件路径操作filesystem
- 使用FileSystem类操作HDFS文件
- php Filesystem
- 使用PHP进行压缩/解压缩文件操作技巧汇总
- boost之filesystem库,目录文件操作函数
- 利用 Hadoop FileSystem moveToLocalFile 方法下载文件 实现HDFS操作
- 利用 Hadoop FileSystem listStatus 遍历文件目录 实现HDFS操作
- HDFS文件操作FileSystem使用API报错:copyToLocalFile NullPointerException
- hadoop利用FileSystem API 执行hadoop文件读写操作
- 利用 Hadoop FileSystem listStatus 遍历文件目录 实现HDFS操作
- php操作php文件
- php filesystem attack
- PHP Filesystem 函数
- PHP Filesystem 函数
- PHP 5 Filesystem 函数
- C# 设置图片透明度源码,支持16/24/32位色深
- 移动的上下左右滑动
- java ip地址相关操作
- Ubuntu下编译C程序的详解
- javascript学习(十四)— 面向对象编程
- php文件操作技巧FileSystem
- jessite 三
- OGG安装及进程理解
- Xamarin.Forms 用户界面——动画
- 170808 逆向-RouterScan(9)
- js随笔
- Smartform中负号提前解决方法
- Hadoop-MapReducer 利用计数器Counters(Java)和Error output(python)计数
- Hibernate配置