files in PHP refer to files and folders, not single-finger files. 1. Determine the file (whether it is a file or a folder) to find the file, the output is file, which represents the document.
Var_dump (FileType ("./aa.txt"));
Find the folder, the output is Dir, which represents the directory.
Var_dump (FileType ("./aa"));
Judging is not a file, the return is true
Var_dump (Is_file ("./aa.txt"));
Judging is not a folder, the return is true
Var_dump (Is_dir ("./aa"));
2. File attributes
Gets the root www of the current server
Gets the last access time of the file, and the timestamp is returned.
echo Date (Fileatime ("./aa.txt"));
Turn the timestamp into time.
echo Date ("Y-m-d h:i:s", Fileatime ("./aa.txt"))
Get File creation time
echo Date ("Y-m-d h:i:s", Filectime ("./aa.txt"));
Get File modification Time
echo Date ("Y-m-d h:i:s", Filemtime ("./aa.txt"));//
Returns the size of the file, in bytes B.
echo filesize ("Aa.txt");
Determines whether the file exists, exists to return true, and does not exist to return flase.
Var_dump (file_exists ("Aa.txt"));
3. File path./Represents the current path: /on behalf of the parent path in the page refers to the document root, the root in PHP is the disk directory, the Web page/also represents the root, is the WWW directory. Returns d:/wamp/www/, which takes the root of the server.
echo $_server[' Document_root '];
Returns a index.php that returns a file name.
Echo basename ("/wamp/www/index.php");
Returns index, which returns only the file name.
Echo basename ("/wamp/www/index.php", ". php");
Returns/WAMP/WWW, just gets the directory.
Echo dirname ("/wamp/www/index.php");
Returns an array that contains information about the directory, file name, file format, and so on.
Var_dump (PathInfo ("/wamp/www/index.php"));
Returns the D:\wamp\www\wenjiancaozuo\aa.txt, converting the relative path to an absolute path.
Echo Realpath ("./aa.txt");
4. Directory operations Create a directory
mkdir ("./bb");
To delete a directory, only empty folders can be deleted. The contents of the folder can not be deleted, will be an error.
RmDir ("BB");
Moving a folder is actually renaming the file to a location.
Rename ("./cc", "./aa/cc");
You can change the file name while moving.
Rename ("./cc", "./aa/dd");
5. Traverse the directory * * Traverse all the files under the rental folder, equivalent to not search within the amount.
Var_dump (Glob ("./zufang/*"));
Traversing files in file format as PHP
Var_dump (Glob ("./zufang/*.php"));
Open the directory resource and always remember to close it when you open it.
$dir = Opendir ("./zufang");
return: Represents the current directory
Echo Readdir ();
Return to ... . represent the parent directory
Echo Readdir ();
Returns the first file
Echo Readdir ();
Returns a second file
Echo Readdir ();
All files are traversed and folders can be read.
while ($f = Readdir ($dir)) { echo $f;}
Close Directory Resource
Closedir ($dir);//
Example 1: Method 1: Give a folder, read the number of files in the folder
Echo Shuliang ("./zufang"), function Shuliang ($filename) { $sum = 0; $dir = Opendir ($filename); while ($f = Readdir ($dir)) { if ($f = = "." | | $f = = "...") If the traversal is. Or ..., nothing is done. { } else { $lujing = $filename. " /". $f; if (Is_file ($lujing)) { $sum + +; } else { $sum = $sum +shuliang ($lujing); }} Closedir ($dir); return $sum; }
Example 1: Method 2: This method is used to get the number of files under the specified folder
function Shuliang ($filename) {$attr = Glob ($filename. ") /* "), $n = count ($attr), foreach ($attr as $v) {if (Is_dir ($v)) {$n = $n +shuliang ($v);}} return $n;} Echo Shuliang ("./0904");
Example 2: Get the size of all the files in a directory
function Fsize ($fname) {$size = 0; $dir = opendir ($fname);//traverse directory, find file, accumulate size while ($u = Readdir ($dir)) {if ($u = = "." | | $u = = "..") {}else{$zfname = $fname. " /". $u, if (Is_file ($zfname)) {$size + = FileSize ($zfname);}} Locate the subdirectory, get the file size Rewinddir ($dir) in the subdirectory, while ($u = Readdir ($dir)) {if ($u = = "." | | $u = = "...") {}else{$zfname = $fname. " /". $u, if (Is_dir ($zfname)) {$size + = fsize ($zfname);}} Closedir ($dir); return $size;} Echo fsize ("./0904");
6. File operations (1) file overall operation create file
Touch ("./cc.txt");
Assignment file, the first parameter is the file to be copied, and the second is where to copy the file.
Copy ("./cc.txt", "./aa/cc.txt");//
deleting files
Unlink ("./cc.txt");//
Read the contents of the file can only read txt, HTML, PHP, and so on, word, wxcle documents can not read.
Echo file_get_contents ("./aa.txt");//
This method is more important, can fetch data from the net, match with regular expression.
Echo file_get_contents ("http://www.baidu.com");//
Write content to the file, and the content will overwrite the original content.
File_put_contents ("./aa.txt", "Hello");//
Modify the contents of the page
File_put_contents ("./ceshi.php", "Hello");//
Get and output, front no write output. File_get_contents need to write output echo.
ReadFile ("http://www.baidu.com");//
Display the Code of the Web page in one line.
Var_dump (File ("http://www.baidu.com"));//
The first parameter is the open file, the second one is open, the way you can view the PHP manual.
$f = fopen ("./aa.txt", "R");//
The content of Aa.txt is hello. This returns the H
echo fgetc ($f);//
Return E
echo fgetc ($f);//
return L
echo fgetc ($f);//
.... fgetc () is read one line at a time
Echo fgets ($f);
Read the length, here is read 2 length, return he.
Echo Fread ($f, 2);
You must close the file when it is opened, and you cannot do anything else without closing it. */
Fclose ($f);
A way is to append
$f = fopen ("./aa.txt", "a");
Write the world behind Hello
Fwrite ($f, "World");
Fclose ($f);
To a folder, delete the folder
Shanchu ("./zufang"), function Shanchu ($filename) { if (Is_dir ($filename))//judgment is file { $dir = Opendir ($ filename);//Open folder while ($f = Readdir ($dir))//Iterate through all contents of the folder { if ($d = = ".") To get rid of . { } else//operation is not. and. The content { $fname = $filename. " /". $f;//Find the traversed content, and spell out the format similar to Wamp/zufang if (Is_file ($fname))//If it is the file { unlink ($fname); } else//if it is a folder { Shanchu ($fname);//The Shanchu method is called again, the deleted folder is changed. } } } Closedir ($dir);//Close folder rmdir ($filename);//Delete the contents of the folder after deleting the folder } else//If it is an empty folder { Unlink ($filename);//delete the Empty folder }}
PHP section--File operation