Let's talk about the basics:
The file location is as follows:
1. Judge whether it is a file or a directory
Var_dump (FileType ("./aa/bb/cc.txt"));
Output: String (4) "File"
Output: String (3) "Dir"
2. Determine if it is a file (return ture)
Var_dump (Is_file ("./aa"));
output bool (FALSE)
3. Determine if it is a directory (return ture)
Var_dump (Is_dir ("./aa"));
Output: bool (TRUE)
4, the file access, creation and modification time
echo Date ("Y-m-d h:i:s", Fileatime ("./aa")); The last access time of the file Filectime ("./aa.txt"); The creation time of the file is Echo date ("Y-m-d h:i:s", Filemtime ("./aa.txt")); When the file was modified
5. Get File size
6. Determine if the file exists
File_exists ("./aa.txt")
7, the root directory of the server
echo $_server[' Document_root '];
Output: d:/phpstudy/www
Note:/represents the root, in the Web page represents the WWW directory, in PHP represents the disk root
8. Path
Echo basename ("./aa/bb/cc.txt"); Gets the file name in the path echo dirname (". /0508/db.class.php "); Gets the folder directory in the path Var_dump (PathInfo (". /0508/db.class.php ")); Get path information echo realpath ("./aa/bb/cc.txt"); Convert relative paths to absolute paths
Output in turn:
Cc.txt
.. /0508
Array (4) { ["dirname"]=> string (7) ": /0508 " [" basename "]=> string ()" DB.class.php " [" extension "]=> string (3)" PHP " [" filename "]=> string (8)" Db.class "}
D:\phpStudy\WWW\2017-05\0519\aa\bb\cc.txt
9. Directory Operations
MkDir ("./aa"); Create directory RmDir ("./aa"); To delete a directory, the directory must be empty rename ("./test", ". /ceshi "); Move Directory
The first kind of traversal directory:
Var_dump (Glob ("./aa/bb/*.txt")); Get all Files under directory
Output:
Array (6) { [0]=> string ()./aa/bb/cc.txt " [1]=> string"./aa/bb/ Dd.txt " [2]=> string"./aa/bb/ee.txt " [3]=> string"./aa/bb/ff.txt " [4]= > string "./aa/bb/gg.txt" [5]=> string "./aa/bb/hh.txt"}
Second Traversal directory: (important)
Open Directory, return directory resource $dname = "./aa/bb"; $dir = Opendir ($dname);//read a file from the directory resource, read one while ($fname = Readdir ($dir)) {echo $dname. /". $fname." <br> ";} Close Directory Resource Closedir ($DIR);
Output:
./aa/bb/.
./aa/bb/.
./aa/bb/cc.txt
./aa/bb/dd.txt
./aa/bb/ee.txt
./aa/bb/ff.txt
./aa/bb/gg.txt
./aa/bb/hh.txt
10, the overall operation of the file
Touch ("./aa.txt"); Create a file copy ("./aa.txt", ".. /aa.txt "); Copy file Unlink ("./aa.txt"); deleting files
11, File content operation
Echo file_get_contents ("http://www.baidu.com"); Read file File_put_contents ("./aa/bb/hh.txt", "Hello"); Write content ReadFile ("./11.txt"); Read and output var_dump (file ("11.txt")); Reads the contents of the file, returns an array, each line is an element
Open File $f = fopen ("./11.txt", "a");//Open file and write to Fwrite ($f, "wwwww");//Close File fclose ($f);
Where: R read-only, r+ read and write, W write empty, w+ read/write, a write to the end of the file, a + read and write; x create and open with write, x+ Create and open with Read and write, plus a B for operable binaries (recommended addition)
Use traversal to calculate all the numbers in a folder
<?phpecho FileCount ("./qiyezhan"); function FileCount ($fname) {////The folder has all the files under $sum = 0;//to determine whether the folder is not the IF (Is_dir ($fname ) {//open folder $dir = Opendir ($fname), while ($wenjian = Readdir ($dir)) {if ($wenjian! = "." && $wenjian! = "...") {//File full path $furl = $fname. " /". $wenjian, if (Is_file ($furl)) {$sum + +;} else if (Is_dir ($furl)) {//Get the number of files under the folder, accumulate $sum = $sum + filecount ($furl);}} Close Folder Closedir ($dir); return $sum;} Else{echo "The folder is not right";}? >
Delete all files with traversal
<?phpshanchu ("./qiyezhan"), function Shanchu ($fname) {if (Is_dir ($fname)) {//before deleting, delete all the files inside $dir = Opendir ($fname while ($dname = Readdir ($dir)) { //must add this item, otherwise the entire disk may be erased if ($dname! = "." && $dname! = "...") {$durl = $fname. " /". $dname; if (Is_file ($durl)) {unlink ($durl);} Else{shanchu ($durl);}} Closedir ($dir);//Delete the folder rmdir ($fname);} else{//if it is a file, delete the unlink ($fname) directly;}? >
How to use PHP to traverse the number of files or delete all files under the directory?