How to get file extension in PHP? Reliable methods and pitfalls
Hello everyone! I just started learning PHP and am writing a small script for uploading images to a server. I need to check the extension of the uploaded file to, for example, allow only JPG and PNG.\n\nI tried using the `explode()` function and taking the last element of the array, but it seems like a hack. Here's my code:\n\n```php\n$filename = 'myphoto.jpg';\n$parts = explode('.', $filename);\n$extension = end($parts);\necho $extension; // jpg\n```\n\nBut what happens if the filename contains multiple dots, for example `my.file.name.jpg`? Or if the file has no extension at all? I've heard there are special functions for this. Please tell me how to correctly and safely get the file extension in PHP? What built-in functions are there and what is best to use in different situations? Thanks in advance for your
No answers yet.
Log in to leave an answer