File Operations in php are much simpler and more powerful than asp operations. For php learning, refer
File Operations in php are much simpler and more powerful than asp operations. For php learning, refer
First, a simple instance is provided.
The Code is as follows:
If (! Is_dir ('txt ') // determines whether the txt file is a folder directory.
{
Mkdir ('txt '); // create a directory named txt
$ Open = fopen ('txt/in.txt ', "w +"); // open a file in read/write mode.
If (is_writable ('txt/in.txt ') // if this file is in writable Mode
{
If (fwrite ($ open, "Today is a wonderful day and you must be happy! --")> 0) // write content
Fclose ($ open); // close the file
Echo "script" alert ('write succeeded '); script "; // a success prompt is displayed.
}
}
Else
{
If (is_file ('txt/in.txt ') // The directory contains the in.txt file.
{
If (is_readable ('txt/in.txt ') // checks whether the file is readable.
{
Echo file_get_contents ('txt/in.txt '); // output text information
Unlink ('txt/in.txt '); // delete the file in.txt
Rmdir ('txt '); // Delete the Directory
}
}
}
?>
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:
The Code is as follows:
$ 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:
Mkdir ($ path, 0777)
, 0777 is the permission code, which can be set by the umask () function in a non-window environment.
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:
$ 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:
$ 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:
$ 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:
$ Fso = fopen ($ cacheFile, 'R ');
$ Data = fread ($ fso, filesize ($ cacheFile ));
Fclose ($ fso );
There is also a way to read binary files:
$ 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:
$ 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:
$ 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:
$ 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:
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;
}
● Copy and delete an object
Php is very easy to delete files. It is easy to use the unlink function:
The Code is as follows:
$ File = 'dirlist. php ';
$ Result = @ unlink ($ file );
If ($ result = false ){
Echo 'mosquito drive away ';
} Else {
Echo 'cannot get rid of it ';
}
?>
You can.
It is also easy to copy files:
The Code is as follows:
$ File = 'ang.txt ';
$ Newfile = 'ji.txt '; # The parent folder of this file must be writable.
If (file_exists ($ file) = false ){
Die ('small samples are not online and cannot be copied ');
}
$ Result = copy ($ file, $ newfile );
If ($ result = false ){
Echo 'Copy memory OK ';
}
?>
You can use the rename () function to rename a folder. Other operations are implemented by combining these functions.
● Get file attributes
I will talk about several common functions:
Obtain the last modification time:
The Code is as follows:
$ File = 'test.txt ';
Echo date ('R', filemtime ($ file ));
?>
The unix timestamp is returned, which is commonly used in cache technology.
Related to obtaining the last access time fileatime (), filectime () when the object's permission, owner, metadata in all groups or other inode is updated, fileowner () function return file owner
$ Owner = posix_getpwuid (fileowner ($ file ));
(Non-window system), ileperms () Get file permissions,
The Code is as follows:
$ File = 'dirlist. php ';
$ Perms = substr (sprintf ('% o', fileperms ($ file),-4 );
Echo $ perms;
?>
Filesize () returns the number of bytes of the file size:
The Code is as follows:
// The output is similar to: somefile.txt: 1024 bytes.
$ Filename = 'somefile.txt ';
Echo $ filename. ':'. filesize ($ filename). 'bytes ';
?>
To get all the information of a file, there is a function stat () that returns an array:
The Code is as follows:
$ File = 'dirlist. php ';
$ Perms = stat ($ file );
Var_dump ($ perms );
?>
For details about the corresponding key, we will not expand it here.
Iv. Conclusion
I have briefly summarized several file operations above. If you are familiar with the functions listed above, there are no major problems during operations. The function operations in the PHP file are fast changing, now it is very powerful, and this part of the file is also a very important part of learning php, hope not to ignore.