PHP folder and file directory operations

Source: Internet
Author: User
Tags glob

PHP folder operation function

String basename (String path [, string suffix])
Give a string containing a full path to a file, this function returns the base file name. If the file name ends in suffix, then this part will be removed as well.
In Windows, slashes (/) and backslashes () can be used as directory separators. In other environments it is a slash (/).

String dirname (String path)

Gives a string containing a full path to a file, which returns the directory name after removing the file name.
In Windows, slashes (/) and backslashes () can be used as directory separators. In other environments it is a slash (/).

Array pathinfo (string path [, int options])

PathInfo () returns a federated array containing information about path. Includes the following array cells: dirname,basename and extension.
You can specify which cells to return through the parameters options. They include: Pathinfo_dirname,pathinfo_basename and Pathinfo_extension. The default is to return all cells.

String Realpath (String path)

Realpath () expands all the symbolic connections and processes the '/./' in the input path, '/'. /' and extra '/' and returns the normalized absolute path name. The path returned does not have a symbolic connection, '/./' or '/'. /' ingredient.
Realpath () returns FALSE when it fails, for example if the file does not exist. On BSD systems, if only path does not exist, PHP does not return FALSE as other systems do.

BOOL Is_dir (string filename)

Returns TRUE if the file name exists and is a directory. If filename is a relative path, the relative path is checked according to the current working directory.
Note: The result of this function is cached. See Clearstatcache () for more information.

Resource Opendir (string path [, resource context])

Opens a directory handle that can be used later in the Closedir (), Readdir (), and Rewinddir () calls.

String Readdir (Resource Dir_handle)

Returns the file name of the next file in the directory. The file name is returned as a sort in the file system.

void Closedir (Resource dir_handle)

Closes the directory stream specified by Dir_handle. The stream must be opened previously by Opendir ().

void Rewinddir (Resource dir_handle)

Resets the directory stream specified by Dir_handle to the beginning of the directory.

Array Glob (string pattern [, int flags])

The Glob () function looks for all file paths that match the pattern in accordance with the rules used by the libc glob () function, similar to the rules used for general shells. No abbreviation extensions or parameter overrides are performed.
Returns an array that contains a matching file/directory. Returns FALSE if an error occurs.

Valid tokens are:
Glob_mark-Add a slash to each returned item
Glob_nosort-Returns (not sorted) according to the original order in which the files appear in the directory
Glob_nocheck-Returns the mode used for search if no file matches
Glob_noescape-Backslash does not escape meta-characters
Glob_brace-expand {a,b,c} to match ' A ', ' B ' or ' C '
Glob_onlydir-Returns only catalog entries that match the pattern

Note: prior to PHP 4.3.3, Glob_onlydir is not available on Windows or other systems that do not use the GNU C library.
Glob_err-Stop and read error messages (such as unreadable directories), ignoring all errors by default
Note: Glob_err is added to PHP 5.1.

PHP File directory Operations

New file
1. First determine what to write to the file
$content = ' Hello ';
2. Open this file (the system will automatically create this empty piece)
Suppose the newly created file is called File.txt and is in the parent directory. W means ' Write file ', $fp below to indicate an open file.
$fp = fopen ('.. /file.txt ', ' W ');
3. Writing a content string to a file
$FP tell the system to write to the file, the content is written $content
Fwrite ($fp, $content);
4. Close the file
Fclose ($FP);
Description: The PHP5 provides a more convenient function file_put_contents, the above 4 steps can be done:
$content = ' Hello ';
File_put_contents (' file.txt ', $content);

deleting files
Delete the files in the Arch directory under the current directory Abc.txt
Unlink (' Arch/abc.txt ');
Note: The system returns the result of the operation, the success returns TRUE, the failure returns FALSE, the variable can be received, you know whether to delete the success:
$deleteResult = unlink (' Arch/abc.txt ');

Get file Contents
Assume that the target file name obtained is file.txt and is in the parent directory. Gets the contents into the $content.
$content = file_get_contents ('.. /file.txt ');

Modify File Contents
The operation method is basically the same as the new content

Renaming a file or directory
Rename the file 1.gif below subdirectory A in the current directory to 2.gif.
Rename ('/a/1.gif ', '/a/2.gif ');
Description: The same is true for directories. The system returns the result of the operation, the success returns TRUE, the failure returns FALSE, and the variable can be received, knowing whether the rename succeeds.
$renameResult = Rename ('/a/1.gif ', '/a/2.gif ');
If you want to move a file or directory, simply set the renamed path to the new path:
Move the file 1.gif below subdirectory A in the current directory to subdirectory b under the current directory, and rename to 2.gif.
Rename ('/a/1.gif ', '/b/2.gif ');
Note, however, that if directory B does not exist, it will fail to move.

Copying files
Copy the file 1.gif under subdirectory A in the current directory to subdirectory b under the current directory, and name it 2.gif.
Copy ('/a/1.gif ', '/b/1.gif ');
Description: This operation cannot be performed on the directory.
If the target file (above/b/1.gif) already exists, the original file will be overwritten.
The system returns the result of the operation, the success returns TRUE, the failure returns FALSE, and the variable can be received to know if the copy was successful.
$copyResult = Copy ('/a/1.gif ', '/b/1.gif ');

Move a file or directory
Action methods and renaming are the same

Whether the file or directory exists
Check if the file logo.jpg is present in the parent directory.
$existResult = File_exists ('.. /logo.jpg ');
Description: Returns False if the file exists system returns TRUE. You can do the same for the directory.

Get File size
Gets the size of the file logo.png under the parent directory.
$size = FileSize ('.. /logo.png ');
Description: The system returns a number that indicates how many bytes (bytes) The size of the file is.

New Catalog
Under directory A in the current directory, create a new directory B.
mkdir ('/a/b ');
Note: The system returns the result of the operation, the success returns TRUE, the failure returns FALSE, and can be received with the variable, knowing whether to create a new success:
$mkResult = mkdir ('/a/b ');

Delete Directory
Delete subdirectory B under directory A in the current directory.
RmDir ('/a/b ');
Note: You can only delete directories that are not empty, or you must first delete subdirectories and files under the directory, and then delete the total directory
The system returns the result of the operation, the success returns TRUE, the failure returns FALSE, and the variable can be received to know if the deletion succeeded:
$deleteResult = rmdir ('/a/b ');

Get all the file names in the directory
1. Open the directory you want to manipulate, and point to it with a variable
Open the directory under the current directory under Pic subdirectory common.
$handler = Opendir (' Pic/common ');
2. Read all the files in the directory under the loop
/* Where $filename = Readdir ($handler) is the file name to be read each time the loop is assigned to $filename, in order not to be trapped in the dead loop, so also let $filename!== false. Be sure to use!==, because if a file name is called ' 0 ', or if some are considered false by the system, you will stop looping with! = *
while (($filename = Readdir ($handler))!== false) {
3, the directory will have two files, the name of '. ' and '. ', do not operate on them
if ($filename! = "." && $filename! = "...") {
4, to deal with
Here, simply use echo to output the file name.
echo $filename;
}
}
5. Close the Directory
Closedir ($handler);

Whether the object is a directory
Check whether the target object logo.jpg under the parent directory is a directory.
$checkResult = Is_dir ('.. /logo.jpg ');
Description: Returns False if the target object is a directory system that returns TRUE. The $checkresult of the above example is of course false.

Whether the object is a file
Check whether the target object logo.jpg under the parent directory is a file.
$checkResult = Is_file ('.. /logo.jpg ');
Description: If the target object is a file, the system returns TRUE, otherwise false is returned. The $checkresult of the above example is of course true.

PHP folder and file 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.