PHP file read, write, and delete operations

Source: Internet
Author: User
Tags flock

 

I. 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:

<? PHP
$ Base_dir = "filelist /";
$ FSO = opendir ($ base_dir );
Echo $ base_dir. "<HR/> ";
While ($ flist = readdir ($ FSO )){
Echo $ flist. "<br/> ";
}
Closedir ($ FSO)
?>
This is to say that the files under the returned file directory are already in the directoryProgram(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.
Dir -- Directory class is also an operationCompositionAn important class of the file directory. There are three methods: read, rewind, and close. This is an object-oriented class. It first uses open file handle, and then read it by Pointer ., see the PHP manual here:

<? 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 important. file attributes include creation time, last modification time, owner, file group, type,
next we will focus on file operations.
2: File Operations
● read a file
first, you 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';
}< BR >?>
file_exists (demonstrated below) is a function used to determine whether a file exists, but is_readable is not comprehensive. You can use this function when a file exists.
$ file = "filelist. PHP ";
If (file_exists ($ file) = false) {
die ('file does not exist ');
}< br> $ 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:
<? 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:
<? 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:
$ F = fopen ($ file, 'w ');
Fwrite ($ F, $ data );
Fclose ($ F );
Replaced.
When writing a file, you sometimes need to lock it, and then write:

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:
<? PHP
$ 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:
<? PHP
$ 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:
<? PHP
$ 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,

<? PHP
$ File = 'dirlist. php ';
$ Perms = substr (sprintf ('% o', fileperms ($ file),-4 );
Echo $ perms;
?>
Filesize () returns the number of bytes of the file size:

<? PHP
// 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:
<? PHP
$ File = 'dirlist. php ';
$ Perms = Stat ($ file );
Var_dump ($ perms );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.