This article will take you through file and directory operations in PHP

青灯夜游
Release: 2023-04-11 09:22:01
forward
5813 people have browsed it

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!

This article will take you through file and directory operations in PHP

Path and disk operations


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
This article will take you through file and directory operations in PHP
The absolute path of this picture is

D:\Google\123\1.png

Let’s explain the content represented by this string of paths separately

  • D:(referring to the drive letter
  • \(the one with the table is the directory separator
  • Tip:(windows operating system Supports forward slashes and backslashes, other systems support forward slashes /, so forward slashes can reduce compatibility issues)

Relative path:With the location of the file to be operated on The directory is the starting path

This is arelative concept, different reference objects write different paths

Why do you say that
Let’s give an example:
Suppose we use1.pngas a reference

This article will take you through file and directory operations in PHP

This article will take you through file and directory operations in PHP

    ##1.txt
  • ./1.txt(There is a dot before 1.)

  • 2.txt
  • ../2.txt(two dots before 2)

    Explain it

  • 1.txt is
  • a file in the same directory, so only one dot is needed in front, and this dot represents the current directory
  • 2.txt is the
  • lower directory, 1.png is theupper directoryof 2.txt, the two dots here represent the upper directory
  • believe it Everyone already knows about relative paths and absolute paths

Getting file path information


We need to understand it first Some concepts

Path:


/love/xianyu.txt

    Directory name (dirname ) is /love
  • with the file extension name (basename) being xianyu.txt
  • the extension name (extension) being .txt
  • the file name (filename) being xianyu

Enter the text below

pathinfo()

In order to obtain

Specified path and file name, you can use thepathinfo()function, this function will return anassociative 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 thefile namepart of the specified filewithout reading from thearray

 /* 中的文件名是:xianyu.php */
Copy after login

Quickly obtain the directory part of the path: dirname()

Use the
dirname()

function toquickly obtain it Specify thedirectorypart of the specified file

 /* /love/xianyu.php中的路径部分是:/love */
Copy after login

Directory operation


Opening and closing directories


Handle:

    opendir(), closedir()
The handle can be considered as a
return value

. With the handle, you can operate on the open directory

Copy after login

Open the images directory

This article will take you through file and directory operations in PHP
This article will take you through file and directory operations in PHP

##Release handle
  • Why release the handle? If you don't release the handle, it's like you and a bunch of people are writing in a notebook. If you take it away and don't put it back, others can't write. So releasing the handle can save server resources
  • 创建目录

    Copy after login

    删除目录

    Copy after login

    获取当前工作目录

    Copy after login

    改变当前工作目录

    '; // 显示当前目录 chdir('hhh'); //改变当前目录 echo getcwd() . '
    '; // 显示当前目录 ?>
    Copy after login

    获取目录句柄条目

    • readdir()

    将一个参数名作为参数传递给函数readdir(),这个函数能返回目录下所有子目录和文件

    注意:调用一次返回一个!!

    Copy after login

    This article will take you through file and directory operations in PHP

    读取目录条目

    • scandir()

    直接返回指定目录下文件和子目录,并且按照要求排序(默认字母升序)

    Copy after login

    This article will take you through file and directory operations in PHP

    This article will take you through file and directory operations in PHP

    文件操作


    操作文件的一般步骤即:打开,读取或写入,关闭

    由于目录和文件本质上都属于文件,除了使用函数不同,很多操作都是相通的

    一般操作

    判断文件是否存在

     //如果文件存在返回true,不存在返回false
    Copy after login

    创建,打开,关闭文件

    • fopen(), fclose()

    Copy after login

    读取文件

    • fread(), file_get_contents(), file()

    Copy after login

    写入文件

    • fwrite(), file_put_contents()

    Copy after login

    文件和目录基本操作

    删除文件

    • unlink()

    Copy after login

    复制文件

    • copy()

    Copy after login

    移动,重命名文件或目录

    • rename()

    Copy after login

    推荐学习:《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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!