PHP 5 classic tutorial file operations. I. INTRODUCTION in any computer device, files are essential objects. in web programming, file operations have always been a headache for web programmers, file operations in the cms system
I. INTRODUCTION
In any computer device, files are essential objects. in web programming, file operations have always been a headache for web programmers, file operations are required and useful in the cms system. we often encounter operations such as file directory generation and File (folder) editing, now I will make a detailed summary of these functions in php and demonstrate how to use them in an example ., for more information about the corresponding functions, see The php Manual. here we will only summarize the key points. and precautions. (This is not available in the php manual .)
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:
$ Base_dir = "filelist /"; $ Fso = opendir ($ base_dir ); Echo $ base_dir ." "; While ($ flist = readdir ($ fso )){ Echo $ flist ." "; } 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:
, 0777 is the permission code, which can be set by the umask () function in a non-window environment.
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:
$ D = dir ("/etc/php5 "); Echo "Handle:". $ d-> handle .""; Echo "Path:". $ d-> path .""; While (false! ==( $ Entry = $ d-> read ())){ Echo $ entry .""; } $ 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
A. read files
First, you can use the is_readable function to obtain the information of a file to check whether the file can be read (Permission problem) or whether the file exists.
$ File = dirlist. php; If (is_readable ($ file) = false ){ Die (the file does not exist or cannot be read ); } Else { Echo exists; } ?> |
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.
$ File = "filelist. php "; If (file_exists ($ file) = false ){ Die (the 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:
$ Fso = fopen ($ cacheFile, r ); $ Data = fread ($ fso, filesize ($ cacheFile )); Fclose ($ fso ); |
There is also a way to read binary files:
$ Data = implode (, file ($ file )); |
B. write files
The same way as reading files, let's see if it can be written:
$ 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:
In any computer device, a file is a necessary object. in web programming, file operations have always been a headache for web programmers, file operations in the cms system...