First, Introduction
In any computer device, files are all necessary objects, and in Web programming, the operation of the file has always been a headache for web programmers, and, the operation of the file in the CMS system is a must, very useful, we often encounter generating files directory, file (clip) editing operations, Now I have a detailed summary of these functions in PHP and examples of how to use them. For a detailed description of the corresponding function, please refer to the PHP manual. Here are just a few things to focus on. And where to pay attention. (This is not in the PHP manual.)
Second, directory operation
The first introduction is a function read from the directory, Opendir (), Readdir (), Closedir (), using the file handle is opened first, and then the iteration is listed:
$base _dir = "filelist/"; $fso = Opendir ($base _dir); echo $base _dir. " " ; while ($flist =readdir ($FSO)) { echo $flist. " " ; } Closedir ($FSO) ?> |
This is the return file directory under which the file is already in the directory program (0 file will return false).
Sometimes you need to know the directory information, you can use DirName ($path) and basename ($path), respectively, return the directory portion of the path and the file name part, the Disk_free_space ($path) to return to see space free space.
To create a command:
, 0777 is the permission code, and the Umask () function can be set under non-window.
The path to the $path file will be deleted.
DIR--The Directory class is also an important class to manipulate the file directory, there are 3 methods, Read,rewind,close, this is a copy object-oriented class, it first uses the open file handle, and then read by the way of the pointer. Here's a look at the PHP manual:
$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 |
The properties of the file are also very important, including the creation time, the last modified time, the owner, the file group, the type, the size, and so on.
Here we focus on file operations.
Third, file operation
A, read the file
The first is a file to see if it can read (permission problems), or there is no, we can use the Is_readable function to obtain information.
$file = dirlist.php; if (is_readable ($file) = = False) { Die (file does not exist or cannot be read); } else { echo exists; } ?> |
The function of judging the existence of a file is also file_exists (shown below), but this is obviously not is_readable comprehensive. When a file exists, it can be used
$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 on earlier versions, you can create a handle to the file first, and then read all with the pointer:
$fso = fopen ($cacheFile, R); $data = Fread ($fso, FileSize ($cacheFile)); Fclose ($FSO); |
There is also a way to read a binary file:
$data = Implode (, file ($file)); |
B, write the file
and read the file the same way, first see if it can write:
$file = dirlist.php; if (is_writable ($file) = = False) { Die ("I'm chicken feathers, I can't"); } ?> |
If you can write it, you can use the File_put_contents function to write:
http://www.bkjia.com/PHPjc/486424.html www.bkjia.com true http://www.bkjia.com/PHPjc/486424.html techarticle introduction in any computer equipment, files are all necessary objects, and in Web programming, the operation of the file has always been a headache for web programmers, and, file operations in the CMS system this ...