PHP/file/name: Difference between revisions
from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
No edit summary |
|||
| Line 10: | Line 10: | ||
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. | * [https://www.php.net/manual/en/function.dir.php {{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. | ||
| Line 22: | Line 22: | ||
* [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 02:28, 9 June 2023
About
PHP's functionality for managing and accessing file/folder names/paths is a mix of classes and standalone functions.
Native Functions
General information:
- Template:Fmt/code: return the path to the current working directory
- This may not be the same as the directory the code is in!
- To get the code's current directory, use the Template:Fmt/code magic constant.
Any of the following can be used to get a directory listing:
- Template:Fmt/code accepts a filepath to an existing folder and returns a Template:Fmt/code 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.
- Template:Fmt/code: see Template:L/same
- Template:Fmt/code accepts a filename and a glob-mask, and tells whether the filename fits the mask
- Template:Fmt/code 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:
- Template:Fmt/code returns the last element of a filespec, with some options.
- Template:Fmt/code returns the filepath to the given file/folder's parent folder.
- Template:Fmt/code breaks a filespec down into multiple components.
- Template:Fmt/code returns a de-contextualized canonical absolute filespec.
Reference
- File System Related Extensions: also includes some Template:L/same-related extensions
- Directories extension
- Directory class
- Template:Fmt/code objects are created with the
dir()method.
- Template:Fmt/code objects are created with the
- Filesystem extension
- This is where all the most common PHP file[sys]-related functions are kept.
Code Snippets
This will get a directory of the folder above the code's current working directory:
$arDir = scandir('../');
echo "Directory:<ul>";
foreach ($arDir as $idx => $fn) {
echo "<li>$fn</li>\n";
}
echo "</ul>";
