Avatar ·

How to get file extension in PHP? (detailed guide)

Hello everyone! I'm a beginner in PHP and can't figure out such a seemingly simple task: how to correctly get the file extension from its name?\n\nI have a variable containing the path to a file, for example:\n```php\n$filename = "images/myphoto.jpg";\n```\nI need to extract only the extension — in this case "jpg". I tried several methods, but they all work incorrectly in different situations.\n\nFor example, I used explode:\n```php\n$parts = explode('.', $filename);\n$ext = end($parts);\n```\nBut if the file name has multiple dots, like "my.file.name.jpg", this method will return "jpg". That seems to work, but is there a more reliable method?\n\nWhat if the file has no extension at all? Or if it's a system file like ".htaccess"? Or if the path contains folders with dots,