Difference between revisions of "PHP/file/name"
Jump to navigation
Jump to search
(moved full "glob()" definition to new /glob subpage; cross-reference link no longer necessary/works) |
|||
Line 3: | Line 3: | ||
==About== | ==About== | ||
PHP's functionality for managing folders is a mix of classes and standalone functions. | PHP's functionality for managing folders is a mix of classes and standalone functions. | ||
− | + | ==Native Functions== | |
Any of the following can be used to get a directory listing: | Any of the following can be used to get a directory listing: | ||
* {{fmt/code|dir()}} accepts a [[filepath]] to an existing folder and returns a {{fmt/code|Directory}} object, which can be iterated through to get the listing. | * {{fmt/code|dir()}} accepts a [[filepath]] to an existing folder and returns a {{fmt/code|Directory}} object, which can be iterated through to get the listing. | ||
** Sorting must be done after retrieving all the files. | ** Sorting must be done after retrieving all the files. | ||
** It seems ''likely'' (I haven't tested this) that the iteration process is atomic by file, i.e. it will return from listing one file even if the directory is damaged and the next one cannot be read. | ** It seems ''likely'' (I haven't tested this) that the iteration process is atomic by file, i.e. it will return from listing one file even if the directory is damaged and the next one cannot be read. | ||
− | * [https://www.php.net/manual/en/function.glob.php {{fmt/code|glob()}}] | + | * [https://www.php.net/manual/en/function.glob.php {{fmt/code|glob()}}]: see {{l/same|glob}} |
− | |||
− | |||
− | |||
* [https://www.php.net/manual/en/function.fnmatch.php {{fmt/code|fnmatch()}}] accepts a filename and a glob-mask, and tells whether the filename fits the mask | * [https://www.php.net/manual/en/function.fnmatch.php {{fmt/code|fnmatch()}}] accepts a filename and a glob-mask, and tells whether the filename fits the mask | ||
* [https://www.php.net/manual/en/function.scandir.php {{fmt/code|scandir()}}] accepts a [[filepath]] to an existing folder and returns an array of files found in the given folder. By default, they are sorted alphabetically. | * [https://www.php.net/manual/en/function.scandir.php {{fmt/code|scandir()}}] accepts a [[filepath]] to an existing folder and returns an array of files found in the given folder. By default, they are sorted alphabetically. | ||
Line 20: | Line 17: | ||
* [https://www.php.net/manual/en/function.pathinfo.php {{fmt/code|pathinfo()}}] breaks a filespec down into multiple components. | * [https://www.php.net/manual/en/function.pathinfo.php {{fmt/code|pathinfo()}}] breaks a filespec down into multiple components. | ||
* [https://www.php.net/manual/en/function.realpath.php {{fmt/code|realpath()}}] returns a de-contextualized canonical absolute filespec. | * [https://www.php.net/manual/en/function.realpath.php {{fmt/code|realpath()}}] returns a de-contextualized canonical absolute filespec. | ||
− | |||
− | |||
− | |||
==Reference== | ==Reference== | ||
* [https://www.php.net/manual/en/refs.fileprocess.file.php File System Related Extensions]: also includes some {{l/same|file}}-related extensions | * [https://www.php.net/manual/en/refs.fileprocess.file.php File System Related Extensions]: also includes some {{l/same|file}}-related extensions |
Revision as of 14:11, 24 August 2022
PHP filesystem access functionality folders/directories and file metadata
|
PHP File fx() |
About
PHP's functionality for managing folders is a mix of classes and standalone functions.
Native Functions
Any of the following can be used to get a directory listing:
- «dir()» accepts a filepath to an existing folder and returns a «Directory» object, which can be iterated through to get the listing.
- Sorting must be done after retrieving all the files.
- It seems likely (I haven't tested this) that the iteration process is atomic by file, i.e. it will return from listing one file even if the directory is damaged and the next one cannot be read.
- «glob()»: see glob
- «fnmatch()» accepts a filename and a glob-mask, and tells whether the filename fits the mask
- «scandir()» accepts a filepath to an existing folder and returns an array of files found in the given folder. By default, they are sorted alphabetically.
The following functions can parse a filespec in various ways:
- «basename()» returns the last element of a filespec, with some options.
- «dirname()» returns the filepath to the given file/folder's parent folder.
- «pathinfo()» breaks a filespec down into multiple components.
- «realpath()» returns a de-contextualized canonical absolute filespec.
Reference
- File System Related Extensions: also includes some file-related extensions
- Directories extension
- Directory class
- «Directory» objects are created with the
dir()
method.
- «Directory» objects are created with the
- Filesystem extension
- This is where all the most common PHP file[sys]-related functions are kept.