This article introduces the content of directory operations in php, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Directory operation
File model division between windows and linux
windows
file file
dir Directory
unknown Unknown
linux
file file
dir Directory
block block
char character
fifo pipe
link link
unknown Unknown
Commonly used directory functions
filesize() Gets the size of the file and returns the result in byte
8bit=1byte
1024b=1KB
1024KB=1MB
1024MB=1GB
1024GB=1TB
file_exists() Determine whether the file or directory exists
filetype() Get the file type
is_dir() Determine whether it is a directory
is_file() determines whether it is a file
is_readable() whether it is readable
is_writeable() whether it is writable
filectime() creation time
fileatime() access time
filemtime() modification time
All timestamps returned
Path to directory
windows: C:\xampp\htdocs
Linux:/usr/local/apache2/htdocs/demo.php
Note
1. The directory separator under windows uses \, while the directory separator under linux uses /. But Windows also recognizes / as the directory separator, so when we write, we always write /
2. Try to use /, because \ represents the escape character in PHP
Directory operations
mkdir() Create directory
First parameter
Path
th Two parameters
There should be no quotation marks when setting permissions (not useful under windows)
The meaning of each part
Owner
Group
Others
Number meaning
r: Readable 4
w: Writable 2
x: Executable 1
The third parameter
Whether to allow recursive directory creation, the default value is false
rmdir() Delete empty directories
unlink() Delete files
dirname( ) Returns the directory name
basename() Returns the file name
pathinfo() Returns an array containing the directory name, file name, suffix name, and base file name
Traverse the directory
1. Open the directory resource
opendir()
Note: opendir Chinese directory cannot be opened To open, you need to use iconv. Convert utf-8 to gbk because the php file is in utf-8 format, but the windows system is in GBK format, so it cannot read it
2. Read from resources
readdir()
Returns the file name of the next file in the directory. When reading to the end, false will be returned
Note
1. In the Windows system, the file names read by readdir of the first and second files are always . and .. ; . represents the current directory, and .. represents the upper-level directory
2. When traversing, deleting, or copying, be sure to exclude . and .
3. Exclusion methods
1. readdir(resource)
readdir(resource)
2. Make a judgment $fileName != '.' && $fileName != '..'
3. Close the directory resource
closedir(resource)
Related recommendations:
Detailed explanation of directory operation examples in PHP
php implements directory operations
The above is the detailed content of Directory operations in php. For more information, please follow other related articles on the PHP Chinese website!