Get Extension From Filename PHP
Last Updated : Jul 1, 2023
IN - 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,
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.
<?php $fileName='Sample.txt'; $extn=pathinfo($fileName,PATHINFO_EXTENSION); echo 'File : '.$fileName.' Path Extention Is :'.$extn; ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
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.