A friend about the introduction of PHP reference to the file and directory of various operating functions and examples of application, the need for friends can be a simple reference.
This chapter is a continuation of the previous chapter, which introduces additional information except the actual contents of the file, including file size, directory, access rights, and so on. Some functions in the file system are only valid when the server is a specific system, such as the function symlink () that changes the symbolic link, the function chmod () that sets the file access permission, the function umask () that sets the Directory Access permission, and so on, which only works in the Linux system. is not valid in Windows system. The Directoryiterator classes provided later in the PHP5 also encapsulate many useful directory-related operations
|
copy code |
//------------- Use files in the Directoryiterator class iteration directory------------- foreach (New Directoryiterator ('/usr/local/images ') as $file) { Print $file->getpathname (). " n "; } //-------------The implementation of the previous version of PHP5------------- $d = opendir ('/usr/local/images ') or Die ($php _errormsg); while (false!== ($f = Readdir ($d))) { Print $f. " n "; } |
Closedir ($d); File information functions
What file information does the function name function provide?
File_exists () whether the file exists
Fileatime () Last Access time
Filectime () file Inode last modified time
Filegroup () Gets a filegroup (returns an integer)
Fileinode () Gets the number of information nodes for the file (returns an integer)
Filemtime () Gets the last time the file data block was written (returns the UNIX timestamp)
Fileowner () Gets the owner of the file (return user ID)
Fileperms () permission to obtain a file
FileSize () The number of bytes to get the file size
FileType () Gets the file type, possibly returning fifo,char,dir,block,link,file and unknown
Is_dir () determines whether a given file name is a directory
Is_executable () to determine if a given file name is executable (available for Windows from PHP5.0.0)
Is_file () determines if the given file name is a normal file
Is_link () determines whether a given file name is a symbolic connection
Is_readable () determines whether a given file name is readable
Is_writable () to determine if a given file name is writable
Directory-related functions
What file information does the function name function provide?
mkdir () Creates a new directory, the second parameter can be used to set access rights
RmDir () Delete directory
Rename () Renaming a file or directory
Directory Class related methods
The Directoryiterator class encapsulates a number of directory-related methods
What directory information does the method name function provide?
Isdir () determines whether the given Directoryiterator item object is a directory
Isdot () determines whether the current Directoryiterator item object is '. ' or ' ... '
Isfile () determines whether the current Directoryiterator item object is a valid file
Islink () determines whether the current Directoryiterator item object is a connection
IsReadable () determines whether the current Directoryiterator item object is readable
IsWritable () determines whether the current Directoryiterator item object is writable
IsExecutable () determines whether the current Directoryiterator item object is executable
Getatime () Gets the current iterator item last accessed time
Getctime () Gets the current iterator item last modified time
Getmtime () Gets the time at which the current iterator item file data block was last written
GetFileName () Gets the current iterator item file name (with extension)
GetPathName () Gets the current iterator item path name
GetPath () Gets the current iterator item pathname and file name
Getgroup () Gets the current iterator item group ID
GetOwner () Gets the current iterator item owner ID
Getperms () Gets the current iterator item permission
GetSize () Gets the current iterator item file size
GetType () Gets the current iterator item type, which may be file,link or dir
Getinode () Gets the inode node number of the current iterator item
File time stamp doubts
Touch () function to modify file update time
The Fileatime () function returns the file because the last time the read or write was opened
The Filemtime () function returns the last time the file contents were modified
The Filectime () function returns the last time the file content or metadata was modified
Get file information
A stat () can be used to get an array of information about the file, similar to this function is the Fstat () function, which takes a file handle as a parameter (returned by fopen () or Popen (), and Lstat () to obtain information about the symbol or file connection.
Numeric index String Index description
0 Dev Device number
1 Ino Information Node number
2 Mode protection
3 Nlink Number of connections
4 UID owner User ID
5 GID Group ID
6 Rdev Device Type, if it is an inode device
7 bytes in size file
8 Atime Last access time (Unix timestamp)
9 Mtime Last modified time (Unix timestamp)
CTime last changed time (Unix timestamp)
Block size of blksize file system IO
The number of blocks occupied by blocks
Modify file Permissions
The chmod () function modifies the permissions of a file
Chown () function modifies the owner of the file
CHGRP () function modifies the group to which the file belongs
Note: The above 3 functions are not valid in Windows systems
Get information about each part of a file name
The basename () function can get the filename, the dirname () function can get the pathname, PathInfo () obtains the directory name, the full file name, the extension, the file name (that is, without the extension) of the associative array, the key name is [DirName], [basename], [ Extension], [filename]
The current directory path (physical path, commonly used in referencing other PHP files) is often obtained through a combination of dirname (__file__).
deleting files
You can delete a file with the unlink () function, and a e_warning error will occur if the deletion fails
Tip: After PHP5.0.0, this function can also be used to delete remote files, such as FTP
Copy or move a file
Using the copy (Old_dir,new_dir) function, you can copy files and move files using rename (Old_dir,new_dir), where the New_dir can rename the file names.
Pattern matching file name list (fuzzy query)
If you want to query all JPG files (*.jpg) in a directory like the command line, you can use the Directoryiterator class's Fileteriterator subclass Accept () method or the Glob () function to get a matching file name.
Implementation code for the Fileteriterator
The code is as follows |
Copy Code |
Class ImageFilter extends Filteriterator { Public Function accept () { return Preg_match (' @. ( gif|jpe?g|png) $@i ', $this->current ()); } } foreach (New ImageFilter (' Directoryiterator ('/usr/local/images ')) as $img) { Print " n "; } |
--------------The implementation code of the GLOB function------------
The code is as follows |
Copy Code |
foreach (Glob ('/usr/local/docs/*.txt ') as $file) { $contents = file_get_contents ($file); Print "$file contains $CONTENTSN";
|
} files in the recursive directory
If you want to get the file size of a directory and its subdirectories, you can use Recursivedirectoryiterator (which provides the functionality obtained by subdirectories) and Recursiveiteratoriterator (flattening)
The code is as follows |
Copy Code |
$dir = new Recursivedirectoryiterator ('/usr/local '); $totalSize = 0; foreach (New Recursiveiteratoriterator ($dir) as $file) { $totalSize + = $file->getsize (); } Print "The total size is $totalSize. N"; |
http://www.bkjia.com/PHPjc/631305.html www.bkjia.com true http://www.bkjia.com/PHPjc/631305.html techarticle a friend about the introduction of PHP reference to the file and directory of various operating functions and examples of application, the need for friends can be a simple reference. This chapter can be considered as the continuation of the previous chapter ...