How does PHP perform file and directory operations? The following article will give you an in-depth understanding of HP file and directory operations. I hope it will be helpful to you!
Relative path and absolute path
Absolute path:The path where the drive letter of the current file is located is the starting point
For example:
For example, picture 1.png
The absolute path of this picture is
D:\Google\123\1.png
Let’s explain the content represented by this string of paths separately
Relative path:With the location of the file to be operated on The directory is the starting path
This is a
relative concept
, different reference objects write different paths
Why do you say that
Let’s give an example:
Suppose we use1.pngas a reference
./1.txt
(There is a dot before 1.)
Explain it../2.txt
(two dots before 2)
We need to understand it first Some concepts
Path:
/love/xianyu.txt
Enter the text below
pathinfo()
In order to obtainSpecified path and file name
, you can use the
pathinfo()function, this function will return an
associative array$value){ echo "$key=>$value\n"; } ?> /* 输出: dirname=>/love basename=>xianyu.php extension=>php filename=>xianyu */Copy after login
Quickly obtain the file name:basename()
##basename()Function,
Quickly obtain the
file namepart of the specified file
without reading from thearray
/* 中的文件名是:xianyu.php */Copy after login
Quickly obtain the directory part of the path: dirname()
Use thedirname()function to
quickly obtain it Specify the
directorypart of the specified file
/* /love/xianyu.php中的路径部分是:/love */Copy after login
return valueOpen the images directory. With the handle, you can operate on the open directory
Copy after login
'; // 显示当前目录 chdir('hhh'); //改变当前目录 echo getcwd() . '
'; // 显示当前目录 ?>
readdir()
将一个参数名作为参数传递给函数
readdir()
,这个函数能返回目录下所有子目录和文件
注意:调用一次返回一个!!
scandir()
直接返回
指定目录下
的文件和子目录
,并且按照要求排序(默认字母升序)
操作文件的一般步骤即:打开,读取或写入,关闭
由于目录和文件本质上都属于文件
,除了使用函数不同,很多操作都是相通的
//如果文件存在返回true,不存在返回false
fopen(), fclose()
fread(), file_get_contents(), file()
fwrite(), file_put_contents()
unlink()
copy()
rename()
推荐学习:《PHP视频教程》
The above is the detailed content of This article will take you through file and directory operations in PHP. For more information, please follow other related articles on the PHP Chinese website!