II. Directory operations
First, we will introduce a function that reads data from a directory, such as opendir (), readdir (), and closedir (). When using this function, we will first open the file handle and then list it through iteration:
The code is as follows: |
Copy code |
<? Php $ Base_dir = "filelist /"; $ Fso = opendir ($ base_dir ); Echo $ base_dir. "While ($ flist = readdir ($ fso )){ Echo $ flist. "<br/> "; } Closedir ($ fso) ?> |
This is a program that returns the files under the file directory that are already in the Directory (0 files will return false ).
If you need to know the directory information, you can use dirname ($ path) and basename ($ path) to return the directory and file name respectively. disk_free_space ($ path) is available) returns the free space of the viewing space.
Create Command:
The code is as follows: |
Copy code |
Mkdir ($ path, 0777) |
, 0777 is the permission code, which can be set by the umask () function in a non-window environment.
The code is as follows: |
Copy code |
Rmdir ($ path) |
The file with the path in $ path will be deleted.
The dir -- directory class is also an important class for operating file directories. It has three methods: read, rewind, and close. This is an object-oriented class. It first uses open file handles, and then read it by pointer ., see the php manual here:
The code is as follows: |
Copy code |
<? Php $ D = dir ("/etc/php5 "); Echo "Handle:". $ d-> handle. "n "; Echo "Path:". $ d-> path. "n "; While (false! ==( $ Entry = $ d-> read ())){ Echo $ entry. "n "; } $ D-> close (); ?> |
Output:
Handle: Resource id #2
Path:/etc/php5
.
..
Apache
Cgi
Cli
File attributes are also very important. File attributes include creation time, last modification time, owner, file Group, type, size, and so on.
Next we will focus on file operations.
III. File operations
● Read files
The first is to check whether a file can be read (Permission problem) or whether the file exists. We can use the is_readable function to obtain information .:
The code is as follows: |
Copy code |
<? Php $ File = 'dirlist. Php '; If (is_readable ($ file) = false ){ Die ('file does not exist or cannot be read '); } Else { Echo 'exist '; } ?> |
The file_exists function is used to determine the existence of the file (as shown below), but this is obviously not comprehensive. It can be used when a file exists.
The code is as follows: |
Copy code |
<? Php $ File = "filelist. php "; If (file_exists ($ file) = false ){ Die ('file does not exist '); } $ Data = file_get_contents ($ file ); Echo htmlentities ($ data ); ?> |
However, the file_get_contents function is not supported in earlier versions. You can create a handle for the file and then read all with pointers:
The code is as follows: |
Copy code |
$ Fso = fopen ($ cacheFile, 'r '); $ Data = fread ($ fso, filesize ($ cacheFile )); Fclose ($ fso ); |
There is also a way to read binary files:
The code is as follows: |
Copy code |
$ Data = implode ('', file ($ file )); |
● Write files
The same way as reading files, let's see if it can be written:
The code is as follows: |
Copy code |
<? Php $ File = 'dirlist. Php '; If (is_writable ($ file) = false ){ Die ("I'm a chicken feather, I can't "); } ?> |
If you can write data, you can use the file_put_contents function to write data:
The code is as follows: |
Copy code |
<? Php $ File = 'dirlist. Php '; If (is_writable ($ file) = false ){ Die ('I am a chicken feather, I can't '); } $ Data = 'I am cool, I want '; File_put_contents ($ file, $ data ); ?> |
File_put_contents functions newly introduced functions in php5 (if you do not know the existence, use the function_exists function to judge first) are unavailable in lower versions of php. You can use the following method:
The code is as follows: |
Copy code |
$ F = fopen ($ file, 'w '); Fwrite ($ f, $ data ); Fclose ($ f ); |
Replaced.
When writing a file, you sometimes need to lock it, and then write:
The code is as follows: |
Copy code |
Function cache_page ($ pageurl, $ pagedata ){ If (! $ Fso = fopen ($ pageurl, 'w ')){ $ This-> warns ('cache file cannot be opened. '); // trigger_error Return false; } If (! Flock ($ fso, LOCK_EX) {// LOCK_NB, locking $ This-> warns ('unable to lock the cache file. '); // trigger_error Return false; } If (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats $ This-> warns ('unable to write the cache file. '); // trigger_error Return false; } Flock ($ fso, LOCK_UN); // release lock Fclose ($ fso ); Return true; } |