Summary of PHP file system processing methods ,. Summary of PHP file system processing methods, this article summarizes and analyzes the php file system processing methods. For your reference, the file type is Linux-based. in the Windows php file system,
This article summarizes and analyzes the php file system processing methods. We will share this with you for your reference. The details are as follows:
File type
In Linux, only file, dir, and unknow types can be obtained in Windows.
In Linux/Unix, block, char, dir, fifo, file, link, and unknown7 types
Block: block setting files, disk partitions, floppy drives, cd-rom, etc.
Char: Character device. I/O (in input and output) devices in character units, such as keyboards and printers.
Dir: Directory is also a type of file/directory file
Fifo: information pipeline, transmitted from one program to another
File: common file types, such as text files, executable files
Link: link file, equivalent to a shortcut in windows
Unknown: unknown type
1. file property processing functions
Filetype ("directory or file name") acquisition type
Is_dir -- determine whether the specified file name is a directory
Is_file -- determine whether a given file name is a normal file
Is_link -- determine whether a given file name is a symbolic connection
Is_executable (); -- determines whether a given file name can be executed
File_exists (); -- whether the file exists
Filesize (); -- returns the file size.
Is_readable (); -- whether the file is readable
Is_writeable (); -- whether the file can be written
Filectime (); -- file creation time
Filemtime (); -- file modification time
Fileactime (); -- last object access time
Stat (); -- file status, returns an array of information about the given file
Bool ftruncate (resource handle, int size );
Accept the file pointer handle as a parameter and truncate the file size into size. If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.
Bool rename (string oldname, string newname [, resource context]);
2. Directory
Directory properties
* Basename (url [, extension]); // returns the file name * dirname (url); // directory name * pathinfo (url); // path information
Example:
$ Path = "/var/www/html/page. php "; echo basename ($ path); // return page. phpecho basename ($ path ,". php "); // pageecho dirname ($ paht); // var/www/html $ arr = pathinfo ($ paht ); $ arr ["dirname"] // var/www/html $ arr ["basename"] // page. php $ arr ["extension"] //. php
Traverse directories
Opendir (url); readdir (url); // return the name of a file with the current directory pointer only, and move the directory pointer back to a closedir (url); rewinddir (url ); // reset the directory pointer to the start position.
Statistics directory size
The statistics directory size can only be set up with recursive functions to add up all the files in the directory;
You can use disk_free_space (url); and disk_total_space (url) to calculate the disk size );
Create and delete directories
Mkdir (url); // create the directory rmdir (url); // delete the empty directory unlink (url); // delete the file
Only recursive functions can be created by yourself to delete non-empty directories;
Copy Directory
Copy ($ scrfile, $ to); // copy an object
User-defined recursive functions are provided to implement Directory Replication.
3. basic file operations
fopen(url);fclose(url);
Write files
Int fwrite (resoure handle, strint string [, int length]);
Returns the number of written characters or FALSE.
Fputs () is the alias of fwrite ()
Int file_put_contents (string filename, string data [, int flags [, resource context]);
It is the same as calling the fopen (), fwrite (), and fclose () functions in turn.
Read files
string fread ( resource handle, int length );
Read a maximum of length bytes from the file pointer handle. This function reads the length of several bytes, or when it reaches the EOF.
string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] );array file ( string filename [, int use_include_path [, resource context]] );
Each unit in the array is a corresponding line in the file, including line breaks.
string fgets ( resource handle [, int length] );string fgetc ( resource handle );int readfile ( string filename [, bool use_include_path [, resource context]] );
Read a file and write it to the output buffer.
To access a remote file, you must activate the "allow_url_fopen" option in the php configuration file to use the fopen () function to open the remote file.
When you use the FTP protocol to connect to a remote file, you can only open the file in "read-only" or "write-only" mode.
Move file pointer
int ftell ( resource handle );
Returns the position of the file pointer specified by handle, that is, the offset in the file stream.
int fseek ( resource handle, int offset [, int whence] );
Set the file pointer position in the file associated with handle. New location, measured in bytes starting from the file header, and offset is added at the position specified by whence. The whence de value is defined:
SEEK_SET-set the position to be equal to the offset byte.
SEEK_CUR-set the position to add offset to the current position.
SEEK_END-set the position to add offset to the end of the file. (To move to the position before the end of the file, you must pass a negative value to the offset .)
bool rewind ( resource handle );
Set the handle file location pointer to the beginning of the file stream