pathinfo()
21st January 2021
0 Comments
Programming Language
PHP [Versions: 4 >= 4.0.3, 5, 7, 8]
Syntax
Typical Usage
Returns information about a file path.
Accepted Parameters
- path (string) (Required): The path to be parsed.
- flags (integer) (Optional) (DefaultĀ = PATHINFO_ALL): Specifies a specific element to be returned. Flags may include any one of the following parameters:
- PATHINFO_DIRNAME
- PATHINFO_BASENAME
- PATHINFO_EXTENSION
- PATHINFO_FILENAME.
Return Value(s)
If the flags parameter is not passed, an associative array containing the following elements is returned: dirname, basename, extension (if any), and filename. Otherwise, returns a string of the request flag.Notes
If the basename of the path starts with a period the filename will be empty.
Examples of pathinfo()
Example 1: Get a File’s Extension in PHP
<?php
$file = '/images/myfile.png';
$ext = pathinfo($file, PATHINFO_EXTENSION);
print_r($ext);
The above will print “png”.
Used In Projects
Was This Helpful?
If this post has helped you, please let me know. Your feedback will be highly appreciated and will help me build better content.