SoFunction
Updated on 2025-04-04

How to get file name and extension in php

php get file name and extension

()-Return the file name of the path

Please see the following php code:

<?php

$path =
"/usr/www/html/";

echo basename($path)."<br>";

//If you select suffix, ignore the extension
echo basename($path,".php");

?>

Running results:

index

()-Return the file path of the current script!

php code:

<?php

//——FILE__Return the full path of the file
$dir = dirname(__FILE__);

echo $dir;

?>

Running results:

F:\web\zend\exercise

() Returns an associative array containing information with path.

Includes the following array units: path name dirname, file name basename, and extension name extension.

Please see the following simple code demonstration:

<?php

$path =
"/usr/www/html/";

$pathinfo 
= pathinfo($path);

echo "Directory name: $pathinfo[dirname]<br>";

echo "File name: $pathinfo[basename]<br>";

echo "Extension: $pathinfo[extension]";

?&gt;

Running results:

Directory name: /usr/www/html

File name:

Extension: php

-- Returns the normalized absolute path name

The php code is as follows:

<?php

$path =
"./exercise/";

$realpath 
= realpath($path);

echo $realpath;

?>

Finally, one tip:File path operators with different paths may be different. You can use "/" and "\" under Windows,

Only "/" can be used in Linux, so when developing, it is recommended to use "/", such as the file path I wrote above!

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.