pathinfo(): get details of a file path
<?Php
//$file = $_SERVER["SCRIPT_NAME"]; // Takes the current file name
$file='https://127.0.0.1/t/file/path.php'; // Path is stored in Variable
$path_details=pathinfo($file);
//echo print_r($path_details);
echo $path_details['dirname']; // Output is: <i>https://127.0.0.1/t/file</i>
echo "<br>";
echo $path_details['basename']; // Output is: <i>path.php</i>
echo "<br>";
echo $path_details['extension']; // Output is: <i>php</i>
echo "<br>";
echo $path_details['filename']; // Output is: <i>path</i>
?>
Syntax
pathinfo(path, flags)
path
:
Required path to be parsed.
flags
:
Optional PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME.
All are returned if not given.
The function pathinfo returns an array.
echo print_r(pathinfo($file,PATHINFO_FILENAME));// output path
Using foreach
$file='https://127.0.0.1/t/file/path.php'; // Path is stored in Variable
$path_details=pathinfo($file);
foreach($path_details as $key => $val) {
echo "$key -> $val <br>";
}
Output
dirname -> https://127.0.0.1/t/file
basename -> path.php
extension -> php
filename -> path
← File
Deleting File → Checking if file exists before deleting →
← PHP
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com