Funny Summary of common PHP file operations functions the following is my summary of PHP file operation functions. Of course, this is only part of it. There are many other parts that I have not listed. 1. Resolution path: 1 get the file name: basename (); Give a string containing a full path pointing to a file. This function returns the basic file name. If the file name is suffix
Funny Summary of common PHP file operations functions the following is my summary of PHP file operation functions. Of course, this is only part of it. There are many other parts that I have not listed. 1. Resolution path: 1 get the file name: basename (); Give a string containing a full path pointing to a file. This function returns the basic file name. If the file name is suffix
Funny Summary of common PHP file operations functions
The following is my summary of the PHP file operation functions. Of course, this is only part of it. There are many other parts that I have not listed.
1. Resolution path:
1. Get the file name:
Basename ();
A string containing a full path pointing to a file is provided. This function returns the basic file name. If the file name ends with suffix, this part will also be removed.
Eg:
$ Path = "/home/httpd/html/index. php ";
$ File = basename ($ path, ". php"); // $ file is set to "index"
2. Obtain the directory:
Dirname ();
A string containing a full path pointing to a file is provided. This function returns the directory name after removing the file name.
Eg:
$ Path = "/etc/passwd ";
$ File = dirname ($ path); // $ file is set to "/etc"
3. Obtain the path Association array.
Pathinfo ();
Get the three parts in a specified path: directory name, basic name, and extension.
Eg:
$ Pathinfo = pathinfo ("www/test/index.html ");
Var_dump ($ pathinfo );
// $ Path ['dirname']
$ Path ['basename']
$ Path ['extension']
Ii. File Type
1. filetype ();
Type of the returned file. Possible values include fifo, char, dir, block, link, file, and unknown.
Eg:
Echo filetype ('/etc/passwd'); // file
Echo filetype ('/etc/'); // dir
3. Obtain the array of useful information for a given file (useful)
1. fstat ();
Get the file information through the opened file pointer
Obtains the statistics of the file opened by the file pointer handle. This function is similar to the stat () function except that it acts on opened file pointers rather than file names.
Eg:
// Open the file
$ Fp = fopen ("/etc/passwd", "r ");
// Obtain statistics
$ Fstat = fstat ($ fp );
// Close the file
Fclose ($ fp );
// Only display the joined Array
Print_r (array_slice ($ fstat, 13 ));
2. stat ()
Obtain the statistical information of the file specified by filename (similar to fstat ())
Iv. Computing size
1. filesize ()
Returns the number of bytes of the file size. If an error occurs, FALSE is returned and an E_WARNING error is generated.
Eg:
// The output is similar to: somefile.txt: 1024 bytes.
$ Filename = 'somefile.txt ';
Echo $ filename. ':'. filesize ($ filename). 'bytes ';
2. disk_free_space ()
Obtains the available space (in bytes) of the disk partition where the directory is located)
Eg
// $ Df contains the number of available bytes in the root directory
$ Df = disk_free_space ("/");
// In Windows:
Disk_free_space ("C :");
Disk_free_space ("D :");
3. disk_total_space ()
Returns the total disk size of a directory.
Eg: (same as above, replace the function)
In addition, if you need to calculate the size of a directory, you can write a recursive function to implement
Code
Function dir_size ($ dir ){
$ Dir_size = 0;
If ($ dh = @ opendir ($ dir )){
While ($ filename = readdir ($ dh ))! = False ){
If ($ filename! = '.' And $ filename! = '..'){
If (is_file ($ dir. '/'. $ filename )){
$ Dir_size + = filesize ($ dir. '/'. $ filename );
} Else if (is_dir ($ dir. '/'. $ filename )){
$ Dir_size + = dir_size ($ dir. '/'. $ filename );
}
}
} # End while
} # End opendir
@ Closedir ($ dh );
Return $ dir_size;
} # End function
V. Access and modification time
1. fileatime (): Last access time
2. filectime (): the last change time (any data modification)
3. filemtime (): Last modification time (that is, content modification only)
Vi. file I/O operations
1. fopen -- open a file or URL
Mode description
'R' is opened in read-only mode. It directs the file pointer to the file header.
Open in 'r + 'read/write mode and point the file pointer to the file header.
Open in 'W' writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
Open in 'W + 'read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
Open the 'A' write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
Open in 'a + 'read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
'X' is created and opened in writing mode. It directs the file pointer to the file header. If the file already exists, fopen () fails to be called and FALSE is returned,
'X + 'is created and opened in read/write mode. The file Pointer Points to the file header. If the file already exists, fopen () fails to be called and FALSE is returned.
Eg:
$ Handle = fopen ("/home/rasmus/file.txt", "r ");
2. file -- read the entire file into an array (this function is very useful)
Like file_get_contents (), only file () is returned as an array. Each unit in the array is a corresponding line in the file, including line breaks. If file () fails, FALSE is returned.
Eg:
Code
$ Lines = file ('HTTP: // www.example.com /');
// Loop in the array to display the HTML source file and add the row number.
Foreach ($ lines as $ line_num => $ line ){
Echo "Line #{$ Line_num}: ". Htmlspecialchars ($ line )."
\ N ";
}
// Another example reads a string from a web page. See file_get_contents ().
$ Html = implode ('', file ('HTTP: // www.example.com /'));
3. fgets -- read a row from the file pointer
Read a row from the file pointed to by handle and return a string of up to length-1 bytes. When a line break (including the returned value), EOF, or the length-1 byte is read, it is stopped ). If length is not specified, the default value is 1 K, or 1024 bytes.
Eg:
$ Handle = @ fopen ("/tmp/inputfile.txt", "r ");
If ($ handle ){
While (! Feof ($ handle )){
$ Buffer = fgets ($ handle, 4096 );
Echo $ buffer;
}
Fclose ($ handle );
}
4. fgetss -- read a row from the file pointer and filter out the HTML Tag
Similar to fgets (), only fgetss tries to remove any HTML and PHP markup from the read text.
You can use the optional third parameter to specify which tags will not be removed.
In addition, the operation on the directory is as follows:
1. opendir -- open the directory handle and open a directory handle, which can be used in subsequent closedir (), readdir (), and rewinddir () calls.
2. readdir -- read entries from the directory handle and return the file name of the next file in the directory. File names are returned in the order in the file system.
Eg:
Code
// Note that it does not exist before 4.0.0-RC2! = Operator
If ($ handle = opendir ('/path/to/files ')){
Echo "Directory handle: $ handle \ n ";
Echo "Files: \ n ";
While (false! ==( $ File = readdir ($ handle ))){
Echo "$ file \ n ";
}
While ($ file = readdir ($ handle )){
Echo "$ file \ n ";
}
Closedir ($ handle );
}
3. scandir -- lists the files and directories in the specified path (useful), and returns an array containing the files and directories in directory.
The default sorting order is ascending by letter. If the optional parameter sorting_order (set to 1) is used, the sorting order is in descending order of letters.
Eg:
$ Dir = '/tmp ';
$ Files1 = scandir ($ dir );
$ Files2 = scandir ($ dir, 1 );
Print_r ($ files1 );
Print_r ($ files2 );
Note:
VII. Operations on file attributes(The operating system environment may be different. Note that)
1. Whether the file is readable:
BoolIs_readable(String filename)
Iffilename
If the specified file or directory exists and is readable, the system returnsTRUE.
Remember, PHP may only use the user name (usually 'nobody') that runs webserver to access the file. This parameter is not included in the security mode.
2File writable?
BoolIs_writable(String filename)
If the file exists and can be written, returnTRUE.filename
The parameter can be a directory name that allows write check.
Remember, PHP may only use the user name (usually 'nobody') that runs webserver to access the file. Not included in security mode restrictions
3Check whether the file exists (This function is very interesting.)
BoolFile_exists(String filename)
Iffilename
If the specified file or directory existsTRUEOtherwise, returnFALSE