PHP file related operation functions--Directory operations

Source: Internet
Author: User
Tags lenovo

1. Functions related to file types

PHP is modeled on UNIX file systems, so we can only get "file", "dir", or "unknown" in the Windows system for three types of files. In Unix systems, we can get 7 types of "block", "Char", "dir", "FIFO", "File", "link" and "Unknown".

1.1 filetype ()

Role: Get the File upload type

Syntax: filetype (filename)

Parameter: The function accepts a file name as a parameter, and returns False if it does not exist.

code example:

echo filetype ("C:\Users\lenovo\Desktop"); Output: Dir

echo filetype ("c:\users\lenovo\desktop/test.txt"); Output: File

1.2 is_file (): Determines whether a given filename is a normal file. Returns False if True is returned

     Is_dir (): Determines whether the given file name is a directory and returns the result as above

    Is_link (): Determines whether the given file name is a symbolic link and returns the result as above.

2. Functions related to file attributes

2.1 file_exists ()

Role: Check whether a file or directory exists

Parameters: File name

Return value: File exists returns True, no return false exists

2.2 filesize ()

Role: Get File size

Parameters: File name

Return value: Returns the number of bytes in the file size, error returns false

2.3 is_readable ()

Function: Determines whether a given file name is readable

Parameters: File name

Return value: Returns True if the file exists and is readable

2.4 is_writable ()

Function: Determines whether a given file name is writable

Parameters: File name

Return value: Returns True if the file exists and is writable

2.5 is_executable ()

Function: Determines whether the given file name is executable

Parameters: File name

Return value: Returns True if the file exists and is executable

2.6 filectime ()

Role: Gets the creation time of the file

Parameters: File name

Return value: Returns the UNIX timestamp format

2.7 filemtime ()

Role: Gets the file modification time

Parameters: File name

Return value: Returns the UNIX timestamp format

2.8 Fileatime ()

Function: Gets the access time of the file

Parameters: File name

Return value: Returns the UNIX timestamp format

2.9 stat ()

Role: Get most of the file property values

Parameters: File name

Return value: Returns an array of useful information about a given file

2.10 Clearstatcache ()

All of the above functions need to provide the same string argument, a string variable that points to a file or directory. PHP will cache the return information of these functions to provide faster performance. However, in some cases, you may want to clear the cached information. For example, if you check the same file multiple times in a script and the file is at risk of being deleted or modified during this script execution, you need to clear the file state cache. You can use this function to clear the file information that is cached by PHP. The Clearstatcache () function caches information for a specific file name, so it needs to be called only if it is repeated multiple times for the same file name and requires that the file information not be cached.

2.11 Lstat (): the function and stat () function are the same, except that the function only works on a symbolic link, not a normal file

2.12 Fstat (): the function and stat () function are the same, except that the function requires a resource handle.

3. Functions related to file directory operations

  3.1 Parsing directory paths

It is often useful to separate attributes from the directory path, such as the extension at the end, the directory part, and the base name.

3.1.1 basename ()

Function: Returns the part of a file in a path

Syntax: String basename (string path[,string suffix])

Parameters: This function gives a string containing a full path to a file, and the second parameter is an optional parameter that specifies the extension of the file. If provided, this extension is not exported.

Return value: This function returns the basic file name

Example code:

$path = "C:\users\lenovo\desktop/test.txt";
Echo basename ($path); Output: Test.txt

Echo basename ($path, ". txt"); Output: Test

3.1.2 dirname ()

Function: Returns the name of the directory that removed the file name

Syntax: striing dirname (String path)

Parameter: The function has only one parameter, giving a string containing a full path to a file

Return value: Returns the name of the directory after removing the file name

Example code:

$path = "C:\users\lenovo\desktop/test.txt";
echo dirname ($path); Output: C:\Users\lenovo\Desktop

3.1.3 pathinfo ()

Function: Return path information

Syntax: PathInfo (path,options)

Return value: Returns an associative array that includes three parts of the directory name, base name, and extension in the specified path, referenced by array keys dirname, basename, and extension, respectively.

Example code:

$path = "C:\users\lenovo\desktop/test.txt";
Var_dump (PathInfo ($path));

Output:array

' dirname ' = string ' C:\Users\lenovo\Desktop ' (length=23)

' basename ' = string ' Test.txt ' (length=8) '

Extension ' = = string ' txt ' (length=3) ' filename ' = = string ' test ' (length=4)

  3.2 Traversing a directory

    To get the files and subdirectories under a directory, you need to use the Opendir () function, the Readdir () function, the Closedir () function, and the Rewinddir () function

3.2.1 Opendir (): Used to open the specified directory, accept the path and directory name of a directory as a parameter, the function return value is a directory handle (resource type) that can be used by other directory functions. Returns False if the directory does not exist or does not have access rights

3.2.2 Readdir (): Used to read the specified directory, accept an actionable directory handle that has been opened with the Opendir () function as a parameter, the function returns a file name for the current directory pointer position, and moves the directory pointer one bit backwards. Returns False when the pointer is at the end of the directory because no file exists.

3.2.3 Closedir (): Closes the specified directory and accepts an actionable directory handle that has been opened with the Opendir () function as a parameter. The function has no return value and will close the Open directory after it is run.

3.2.4 Rewinddir (): Returns the directory handle, accepting an actionable directory handle that has been opened with the Opendir () function as a parameter. Resets the directory pointer to the beginning of the directory, back to the beginning of the directory.

Example code:

$dirname = ' C:\Users\lenovo\Desktop ';
$dir _handle = Opendir ($dirname);
while ($file = Readdir ($dir _handle))
{
$dirFile = $dirname. " /". $file;
echo $file. " ". FileSize ($dirFile)." ". filetype ($dirFile);
echo "<br>";
}
Closedir ($dir _handle);

  3.3 Statistics Directory Size

PHP currently does not provide a standard function for the total size of the directory, so we want to customize a function to accomplish this task. The first thing to consider is that there are no other subdirectories in the directory, and if there are no subdirectories, the sum of all the files in the directory is the size of the directory. If a subdirectory is included, the size of the directory will be computed in this way. Using recursive functions is best suited for this task. Code See P351

  3.4 Creating and deleting directories

3.4.1 mkdir ()

This function is used to create a new directory, details access: http://www.w3school.com.cn/php/func_filesystem_mkdir.asp

3.4.2 rmdir ()

The function can only delete an empty directory and the directory must exist. If a non-empty directory needs to go into the directory first, use the unlink () function to delete each file in the directory and then return to the empty directory. If subdirectories are still present in the directory, and the subdirectories are not empty, then recursive methods will be used. See P352 for detailed code

  3.5 Copying a directory

Although copying a directory is the basic function of a file operation. However, there is no specific function in PHP, and it is also necessary to customize a recursive function implementation. To copy a directory containing a multilevel subdirectory, which involves copying files, creating directories, copying a file can be done with the copy () function provided by PHP, and creating a directory using the mkdir () function. When defining a function, the source directory is traversed first, and if it encounters a normal file, it is copied directly using the copy () function. If the traversal is encountering a directory, you must establish the directory, and then copy the files under that directory, and if there are subdirectories, use recursive repetition, eventually copying the entire directory, and customizing the recursive function to copy the directory code see P352

  

PHP file related operation functions--Directory operations

Related Article

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.