PHP之基本目录操作
一、创建目录
mkdir ($pathname, $mode = 0777, $recursive = false, $context = null)
- $pathname: 目录路径
- $mode : 目录权限
- $recursive : 递归
二、删除目录
rmdir ($dirname, $context = null)
- $dirname: 目录路径
三、读取目录内容
步骤:
3.1、打开目录,获取文件句柄
$fin = opendir($path);
var_dump($fin);
resource(2) of type (stream)
3.2、读取内容
while (false !== ($dir_name = readdir($dir_handler)))
如果读取内容不为空,继续读取
3.3、关闭文件句柄
closedir($dir_handler);
用于释放资源
四、递归之目录结构
4.1、读取目录结构
4_1代码:
$path = '../secshop_upload';//目录或者文件路径
recursiveDir($path);
function recursiveDir($pathname, $depth = 0) {
$dir_handler = opendir($pathname);
while (false !== ($dir_name = readdir($dir_handler))) {
if ($dir_name == '.' || $dir_name == '..') continue;
echo str_repeat('-', $depth).$dir_name . '<br />';
if (is_dir($pathname.DIRECTORY_SEPARATOR.$dir_name)) recursiveDir($pathname.DIRECTORY_SEPARATOR.$dir_name, $depth +1);
}
closedir($dir_handler);
}
结果:
application
-back
--controller
---GoodsController.class.php
---LoginController.class.php
---ManageController.class.php
---PlatformController.class.php
--model
---AdminModel.class.php
---GoodsModel.class.php
--view
...
4.2、获取目录结构嵌套数组
$path = '..'.DIRECTORY_SEPARATOR.'secshop_upload';//目录或者文件路径
$arr = recursiveDir($path);
echo '<pre>';
var_dump($arr);
echo '</pre>';
/**
* @param $pathname 目录路径
* @return array 嵌套数组:递归遍历目录内容存入数组
*/
function recursiveDir($pathname) {
$nested_arr = array();//存放当前目录下内容 f
$index = 0;
$dir_handler = opendir($pathname);//打开目录,获取文件句柄
while (false !== ($dir_name = readdir($dir_handler))) {//循环读取目录内容
if ($dir_name == '.' || $dir_name == '..') continue;//'.' 和'..'为逻辑目录
$nested_arr[$index]['filename'] = $dir_name;//‘filename'目录或者文件名
if (is_dir($pathname.DIRECTORY_SEPARATOR.$dir_name)) {
$nested_arr[$index]['type'] = 'DIR';//type 类型: DIR 目录;FILE 文件
//nested 子目录内容数组
$nested_arr[$index]['nested'] = recursiveDir($pathname.DIRECTORY_SEPARATOR.$dir_name);
}else {
$nested_arr[$index]['type'] = 'FILE';
}
$index++;
}
closedir($dir_handler);//关闭文件句柄,是否资源
return $nested_arr;//目录内容数组返回
}
结果:
array(6) {
[0]=>
array(3) {
["filename"]=>
string(11) "application"
["type"]=>
string(3) "DIR"
["nested"]=>
array(4) {
[0]=>
array(3) {
["filename"]=>
string(4) "back"
["type"]=>
string(3) "DIR"
["nested"]=>
array(3) {
[0]=>
array(3) {
["filename"]=>
string(10) "controller"
["type"]=>
string(3) "DIR"
["nested"]=>
array(4) {
[0]=>
array(2) {
["filename"]=>
string(25) "GoodsController.class.php"
["type"]=>
string(4) "FILE"
}
五、中文路径
iconv ($in_charset, $out_charset, $str)
- $in_charset: The input charset
- $out_charset: output charset
- $str : 要转换的字符串
通过字符集转换,解决乱码等问题
PHP之基本目录操作的更多相关文章
- 【C#公共帮助类】FTPClientHelper帮助类,实现文件上传,目录操作,下载等动作
关于本文档的说明 本文档使用Socket通信方式来实现ftp文件的上传下载等命令的执行 欢迎传播分享,必须保持原作者的信息,但禁止将该文档直接用于商业盈利. 本人自从几年前走上编程之路,一直致力于收集 ...
- liunx学习(一):linux下目录操作大全
Linux C函数之文件及目录函数(全):http://blog.sina.com.cn/s/blog_695e489c01013ldd.html linux目录操作发:http://www.cnbl ...
- Java基础知识系列——目录操作
Java对目录操作的许多方法与上一篇文件操作的方法很多是一样的. java.io.File file = new File( "D:\1\2\3\4"); 1.递归创建目录 fil ...
- Python目录操作
Python目录操作 os和os.path模块os.listdir(dirname):列出dirname下的目录和文件os.getcwd():获得当前工作目录os.curdir:返回但前目录('.') ...
- PHP 文件与目录操作函数总结
>>>文件操作 打开 fopen(); 打开文件 读取内容 fread(); 从文件指针 handle 读取最多 length 个字节 readfile(); 读入 ...
- Python::OS 模块 -- 文件和目录操作
os模块的简介参看 Python::OS 模块 -- 简介 os模块的进程管理 Python::OS 模块 -- 进程管理 os模块的进程参数 Python::OS 模块 -- 进程参数 os模块中包 ...
- Matlab命令系列之目录操作
Matlab命令系列之目录操作 filesep 用于返回当前平台的目录分隔符,Windows是反斜杠(),Linux是斜杠(/).有时此命令结合ispc命令使用,可以灵活的设置目录分割符. fullf ...
- Matlab命令——目录操作(windows&Linux)
Matlab命令——目录操作(windows&Linux) 1. filesep用于返回当前平台的目录分隔符,Windows是反斜杠(\),Linux是斜杠(/).有时此命令结合ispc命令使 ...
- 【Linux C中文函数手册】之 目录操作函数
目录操作函数 1)closedir 关闭目录 相关函数: opendir表头文件: #include<sys/types.h> #include<dirent.h>定义函数: ...
- OC7_目录操作
// // main.m // OC7_目录操作 // // Created by zhangxueming on 15/6/19. // Copyright (c) 2015年 zhangxuemi ...
随机推荐
- postgresql导出某张表的数据
\copy 表名 to 路径 with csv 比如: \copy dataset to /home/backup/dataset.csv with csv \copy dataset to /hom ...
- PHP 中获取文件名及路径
1. basename("/mnt/img/image01.jpg")函数:得到文件名;输出结果为:image01.jpg. 使用 basename($uriString) 我们可 ...
- web小trick
1.linux下交换文件 .index.php.swp 有时可查看源码2.当php后缀被过滤的时候可以直接对ph开头的后缀进行一个fuzz测试可以上传的文件后缀名3.curl -x 123.45.67 ...
- Rspec基本语法
引用链接:http://reverocean.iteye.com/blog/1489957 1. describe和context describe和context方法用来组织相关的行为example ...
- 使用Fsharp 探索 Dotnet 平台
Fsharp的交互开发环境使得我们在了解DotNet平台时能够快速的获得需要的反馈. 反馈在任何技艺的磨练过程中必不可少,我认为也是最重要的环节之一.在“一万小时天才理论”中,著名的髓鞘质就是在快速有 ...
- 转载:Maven实战—Dependencies与DependencyManagement的区别
致敬作者,支持原创.原文地址:https://www.cnblogs.com/feibazhf/p/7886617.html 在上一个项目中遇到一些Jar包冲突的问题,之后还有很多人分不清楚Depen ...
- webpack实用小功能介绍
1.overlay overlay属于devServer的属性,配置案例如下: ? 1 2 3 4 5 6 devServer: { overlay: { errors: true, war ...
- chart.js 使用方法 特别说明不是中文的
以上是一个饼图的案例,其他统计类型查看文档 http://www.chartjs.org/docs/latest/charts/doughnut.html 注意看域名 chartjs.org 不是 ...
- 编程中的多字节和Unicode
在编译许多程序的时候,我们常常会出现诸如指针转换错误或者const char[] 不能转换成XX的错误,这时很可能就是项目编码的问题了,如果您使用的是VS编程环境,那么打开工程属性,里面就有个选项是给 ...
- 【MFC】将当前的日期转化为1970年开始的秒计数
CTime time1 = CTime::GetCurrentTime(); int nTSeconds = time1.GetTime(); CTime time2(,,,,,); nTSecond ...