All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Get Extension From Filename PHP

Last Updated : Mar 11, 2024

Get Extension From Filename PHP

In this article we will show you the solution of get extension from filename php, through pathinfo() method we can collect every detail about a particular file easily in php.

Generally pathinfo() method gives information all about specified file. Details including directory name, file name and extension returned when passing a file to this method.

Based on your requirement you can retrieve any detail but one thing way of specification has to be inbuilt rules to achieve error-free output.

To know in detail with same code without skip keep read below.

Step By Step Guide On Get Extension From Filename PHP :-

Foremost step you need to define a variable to store a sample file that can be exist or not.

Both will return extension of given file instantly. Specifically to retrieve extension follow below giving syntax and necessitate pattern,

Syntax :-

pathinfo(filename saved variable, PATHINFO_EXTENSION);

It will return the file extension to store or display the retrieved result define a variable at left hand side and use it to access the result.

echo 'File : '.$fileName.' Path Extention Is :'.$extn;
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
  3. In the sample code, we defined variable ‘$fileName’ to store input or value of file name ‘Sample.txt’. This Sample.txt file not mandatory to present in your local device storage.
  4. Using pathinfo() we should pass two parameters ($fileName-source input, PATHINFO_EXTENSION). It helps you to attain result of extension of given input file without fail.
  5. To avoid doubt with the help of echo statement printed result in detail, refers to gave input and retrieved answer that lead you to understand and verify the use of code.
  6. Here we provided text file that’s why it gives output ‘txt’, if you changed ‘txt to html’ as per your changes you will get result flawlessly.
  7. Instead echo() you can prefer print() method if you want and that not going to affects your result. Still some type of input exist you can try those along with it to master your mind in this concept.

Conclusion :-

In conclusion, hope now you know how to get extension of any file without flaws.

When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.

Otherwise sometimes you will get error so ensure it if you get any error while running output.

When we execute this program on browser it will print ‘File : Sample.txt Path Extension Is :txt’ as output.

Like say earlier appropriate your input files it returns extension correctly at browser.

In that case, with this help of sample code you can test all types of files.

I hope this article on get extension from filename php helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪