Difference between revisions of "PHP/file/name"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP‎ | file
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{fmt/title|PHP filesystem access functionality|folders/directories and file metadata}}
+
{{fmt/title|PHP filename functionality|folders/directories/paths}}
 +
{{:PHP/file/NAV}}
 
==About==
 
==About==
PHP's functionality for managing folders is a mix of classes and standalone functions.
+
PHP's functionality for managing and accessing file/folder names/paths is a mix of classes and standalone functions.
 +
==Native Functions==
 +
General information:
 +
* [https://www.php.net/manual/en/function.getcwd.php {{fmt/code|getcwd()}}]: 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 {{fmt/code|__DIR__}} [https://www.php.net/manual/en/language.constants.magic.php magic constant].
  
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.
+
* [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.
* [https://www.php.net/manual/en/function.glob.php {{fmt/code|glob()}}] accepts a [[filespec]] that includes a [[file mask]], and returns a list of matching [[filespec]]s.
+
** Sorting must be done after retrieving all the files.
** A file mask in this context can be e.g. {{fmt/code|*.php}} or {{fmt/code|/home/woozle/*.php}} but not {{fmt/code|~/*.php}}.
+
** 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.
** The {{fmt/code|*}} and {{fmt/code|?}} wildcards are both recognized.
+
* [https://www.php.net/manual/en/function.glob.php {{fmt/code|glob()}}]: see {{l/same|glob}}
** The mask may include multiple wildcards, for folders as well as files. '''Example''': {{fmt/code|/b*/b*}} will return all files beginning with b inside root-level folders beginning with b.
+
* [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.
  
To parse a [[filespec]]:
+
The following functions can parse a [[filespec]] in various ways:
 
* [https://www.php.net/manual/en/function.basename.php {{fmt/code|basename()}}] returns the last element of a filespec, with some options.
 
* [https://www.php.net/manual/en/function.basename.php {{fmt/code|basename()}}] returns the last element of a filespec, with some options.
 
* [https://www.php.net/manual/en/function.dirname.php {{fmt/code|dirname()}}] returns the filepath to the given file/folder's parent folder.
 
* [https://www.php.net/manual/en/function.dirname.php {{fmt/code|dirname()}}] returns the filepath to the given file/folder's parent folder.
 
* [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.
 +
** There doesn't seem to be any corresponding function for rebuilding a filespec from an array.
 
* [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.
  
==Related==
 
* {{l/same|file}}: file access functions
 
 
==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
Line 25: Line 31:
 
** [https://www.php.net/manual/en/book.filesystem.php Filesystem extension]
 
** [https://www.php.net/manual/en/book.filesystem.php Filesystem extension]
 
*** This is where all the most common PHP file[sys]-related functions are kept.
 
*** 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:
 +
<syntaxhighlight lang=php>
 +
$arDir = scandir('../');
 +
echo "Directory:<ul>";
 +
foreach ($arDir as $idx => $fn) {
 +
    echo "<li>$fn</li>\n";
 +
}
 +
echo "</ul>";
 +
</syntaxhighlight>

Latest revision as of 21:20, 4 July 2023

PHP filename functionality
folders/directories/paths

PHP File fx()

About

PHP's functionality for managing and accessing file/folder names/paths is a mix of classes and standalone functions.

Native Functions

General information:

  • «getcwd()»: 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 «__DIR__» magic constant.

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.
    • There doesn't seem to be any corresponding function for rebuilding a filespec from an array.
  • «realpath()» returns a de-contextualized canonical absolute filespec.

Reference

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>";