10个最重要的PHP目录函数
来源:互联网 发布:网络视频测试 编辑:程序博客网 时间:2024/05/13 22:46
当我们在编写PHP应用程序时,免不了会有处理文件的模块,此时读取或创建文件夹、文件是必不可少的,其实这并不复杂,只要我们使用PHP内置的文件函数即可。本文列出了10个常用、重要,也是必须掌握的文件函数。
创建目录函数:mkdir()
使用mkdir()函数可以在指定路径下创建目录,并且可以设置目录权限,如:
1
mkdir
(
"/home/php_files"
, 0777);
在home目录下创建php_files目录,第二个参数可选。
获取当前文件目录函数:getcwd()
在PHP中,使用getcwd()函数可以获取当前文件所在的目录
改变当前文件目录:chdir()
使用chdir()函数可以改变文件当前目录,我们只需要给出一个新的路径即可,如:
1
chdir
(
'include'
);
将当前文件路径转到子目录include下。
改变根目录:chroot()
chroot()函数可以将当前文件目录更改为根目录“/”,如:
1
2
3
4
<?php
chroot
(
"/home/php_filesinclude/include"
);
echo
getcwd
();
?>
输出“/”
读取目录内容:opendir()
使用opendir()函数打开目录,并配合readdir()函数读取目录内容。如:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$dir
=
"/home/php_files/images/"
;
// Open up a directory, and read its contents
if
(
is_dir
(
$dir
)){
if
(
$dh
= opendir(
$dir
)){
while
((
$file
= readdir(
$dh
)) !== false){
echo
"File:"
.
$file
. "
";
}
closedir
(
$dh
);
}
}
?>
重置目录句柄:rewinddir()
rewinddir()函数可以重置由opendir()打开的目录句柄。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$dir
= opendir(
"/home/php_files/images/"
);
if
(
$dh
= opendir(
$dir
)){
while
((
$file
= readdir(
$dh
)) !== false){
echo
"filename:"
.
$file
. "
";
}
rewinddir
();
while
((
$file
= readdir(
$dh
)) !== false){
echo
"filename:"
.
$file
. "
";
}
}
?>
文件和目录列表:scandir()
使用scandir()函数可以查询指定目录的目录和文件列表。
1
2
3
4
5
<?php
$dir
= opendir(
"/home/php_files/images/"
);
$files
= scandir(
$dir
);
print_r(
$files
);
?>
获取目录对象:dir()
dir()函数打开一个目录句柄,并返回一个目录流对象。
1
2
3
4
<?php
$path
= dir(
getcwd
());
print_r(
$path
);
?>
打开目录句柄:opendir()
通过opendir()函数打开目录,并读取文件。
1
2
3
4
5
6
7
8
9
10
11
12
<?php
$dir_path
= dir(
getcwd
());
if
(
is_dir
(
$dir_path
)){
if
(
$dh
= opendir(
$dir_path
)){
while
((
$file
= readdir(
$dh
)) !== false){
echo
"File:"
.
$file
. "
";
}
closedir
(
$dh
);
}
}
?>
关闭目录句柄:closedir()
目录句柄打开并使用完成后最好能手动关闭,以节省资源。
1
2
3
4
5
6
7
8
9
<?php
$dir_path
= dir(
"/home/php_files/"
);
if
(
is_dir
(
$dir_path
)) {
if
(
$dh
= opendir(
$dir_path
)) {
$directory
= readdir(
$dh
);
closedir
(
$dh
);
}
}
?>
0 0
- 10个最重要的PHP目录函数
- 数据库管理10个最重要的特点
- 软件工程中10个最重要的理念
- 软件工程中10个最重要的理念
- 软件工程中10个最重要的理念
- 软件工程中10个最重要的理念
- 软件工程中10个最重要的理念
- 软件工程中10个最重要的理念
- 软件工程中10个最重要的理念
- 软件工程中10个最重要的理念
- 软件工程中10个最重要的理念
- 软件工程中10个最重要的理念
- 10个最重要的开源软件
- Java Array最重要的10个方法
- 软件工程中10个最重要的理念
- 最重要的30个算法
- 最重要的32个算法
- 32个最重要的算法
- 第十一章--内核的数据类型
- 开始建设discuz论坛的时候,最艰难的就是对于整个论坛架构的掌握
- c和c++
- 骑士走棋盘
- 作业
- 10个最重要的PHP目录函数
- iOS 7系列译文:忘记NSURLConnection,拥抱NSURLSession吧!
- 最高分存储
- OpenDaylight OpenFlow1.3测试环境
- js 三级联动学习记录
- 银行降转账额度 “余额宝”要当心了
- python 打包
- 关于微型真空泵与气体采样泵的区别
- Java Web开发下载文件,文件名中文乱码解决方案