Difference between revisions of "PHP/file/name"
Jump to navigation
Jump to search
m (Woozle moved page PHP/filesystem to PHP/file/sys without leaving a redirect: reorg) |
|
(No difference)
|
Revision as of 21:24, 30 June 2022
PHP filesystem access functionality folders/directories and file metadata
|
About
PHP's functionality for managing folders is a mix of classes and standalone 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()» accepts a filespec that includes a file mask, and returns a list of matching filespecs.
- A file mask in this context can be e.g. «*.php» or «/home/woozle/*.php» but not «~/*.php».
- The «*» and «?» wildcards are both recognized.
- The mask may include multiple wildcards, for folders as well as files. Example: «/b*/b*» will return all files beginning with b inside root-level folders beginning with b.
- «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.
Related
- file: file access functions
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.