Active1 month ago
This is a question you can read everywhere on the web with various answers:
In the following table, you can find a list of programs that can open files with.php extension.This list is created by collecting extension information reported by users through the 'send report' option of FileTypesMan utility. Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions. Other PHP.net sites. The PHP 3 Web Page file type, file format description, and Mac, Windows, and Linux programs listed on this page have been individually researched and verified by the FileInfo team. We strive for 100% accuracy and only publish information about file formats that we have tested and validated.
etc.
However, there is always 'the best way' and it should be on Stack Overflow.
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
e-satise-satis376k9898 gold badges271271 silver badges311311 bronze badges
26 Answers
People from other scripting languages always think theirs is better because they have a built-in function to do that and not PHP (I am looking at Pythonistas right now :-)).
Developed with JetFX, HP Indigo GEM is available to pre-order at Labelexpo Europe 2017. Cara membuat power supply digital buat hp.
In fact, it does exist, but few people know it. Meet
pathinfo()
:This is fast and built-in.
pathinfo()
can give you other information, such as canonical path, depending on the constant you pass to it.Remember that if you want to be able to deal with non ASCII characters, you need to set the locale first. E.G:
Also, note this doesn't take into consideration the file content or mime-type, you only get the extension. But it's what you asked for.
Lastly, note that this works only for a file path, not a URL resources path, which is covered using PARSE_URL.
Enjoy
Sayed Mohd Ali1,81133 gold badges66 silver badges2121 bronze badges
e-satise-satis376k9898 gold badges271271 silver badges311311 bronze badges
T.Todua34.3k1212 gold badges151151 silver badges145145 bronze badges
Adam WrightAdam Wright45.6k99 gold badges125125 silver badges146146 bronze badges
Example URL:
http://example.com/myfolder/sympony.mp3?a=1&b=2#XYZ
Note: hashtags are not available in server-side, only if manually added.
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
T.ToduaT.Todua34.3k1212 gold badges151151 silver badges145145 bronze badges
There is also
SplFileInfo
:Often you can write better code if you pass such an object around instead of a string. Your code is more speaking then. Since PHP 5.4 this is a one-liner:
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
hakrehakre164k3333 gold badges323323 silver badges637637 bronze badges
E-satis's response is the correct way to determine the file extension.
Alternatively, instead of relying on a files extension, you could use the fileinfo to determine the files MIME type.
Here's a simplified example of processing an image uploaded by a user:
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
ToxygeneToxygene
1) If you are using (PHP 5 >= 5.3.6)you can use SplFileInfo::getExtension — Gets the file extension
Example code
This will output
2) Another way of getting the extension if you are using (PHP 4 >= 4.0.3, PHP 5) is pathinfo
Example code
This will output
// EDIT: removed a bracket
Subodh GhulaxeSubodh Ghulaxe14.8k1212 gold badges7272 silver badges9494 bronze badges
As long as it does not contain a path you can also use:
Where
Peter Mortensen$fname
is a name of the file, for example: my_picture.jpg
.And the outcome would be: jpg
14.4k1919 gold badges8888 silver badges117117 bronze badges
AnonymousAnonymous2,54344 gold badges4545 silver badges7979 bronze badges
Sometimes it's useful to not to use
pathinfo($path, PATHINFO_EXTENSION)
. For example:Also note that
Peter Mortensenpathinfo
fails to handle some non-ASCII characters (usually it just suppresses them from the output). In extensions that usually isn't a problem, but it doesn't hurt to be aware of that caveat.14.4k1919 gold badges8888 silver badges117117 bronze badges
Alix AxelAlix Axel111k7272 gold badges353353 silver badges468468 bronze badges
The simplest way to get file extension in PHP is to use PHP's built-in function pathinfo.
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
ShahbazShahbaz2,70811 gold badge2020 silver badges3939 bronze badges
You can try also this (it works on PHP 5.* and 7):
Tip: it returns an empty string if the file doesn't have an extension
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
pooya_sabramoozpooya_sabramooz
Kurt ZhongKurt Zhong5,63811 gold badge1414 silver badges1414 bronze badges
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
Php Get File Extension
version 2version 259311 gold badge1010 silver badges2525 bronze badges
pathinfo is an array. We can check directory name, file name, extension, etc.:
Resetter epson r230 rar. The console is the epson r230 reset same chiclet style as you'll discover on most portable PCs today, yet with a satisfying measure of key travel (more than the new Retina MacBook, in any case). The screen is sufficient to get its own particular yell out. There's the absolute minimum of ports: one USB-C on each side, a devoted volume and power key, a microSD space, and one earphone jack. Double USB-C implies you can connect the reseter epson r230 charger to on either side, and gives you simply enough ports to charge the tablet and module a hard drive in the meantime.
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
Arshid KVArshid KV6,19433 gold badges2323 silver badges2828 bronze badges
Here is an example. Suppose $filename is 'example.txt',
So $ext will be '.txt'.
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
G. I. JoeG. I. Joe
Deepika PatelDeepika Patel1,81222 gold badges1414 silver badges1212 bronze badges
I found that the
pathinfo()
and SplFileInfo
solutions works well for standard files on the local file system, but you can run into difficulties if you're working with remote files as URLs for valid images may have a #
(fragment identifiers) and/or ?
(query parameters) at the end of the URL, which both those solutions will (incorrect) treat as part of the file extension.I found this was a reliable way to use
Jonathan EllisJonathan Ellispathinfo()
on a URL after first parsing it to strip out the unnecessary clutter after the file extension:3,26522 gold badges2727 silver badges4646 bronze badges
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
smile 22121smile 22121
Use
substr($path, strrpos($path,'.')+1);
. It is the fastest method of all compares.@Kurt Zhong already answered.
Let's check the comparative result here: https://eval.in/661574
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
AbbasAbbas
If you are looking for speed (such as in a router), you probably don't want to tokenize everything. Many other answers will fail with
Ray FossRay Foss/root/my.folder/my.css
2,35611 gold badge1616 silver badges2121 bronze badges
Although the 'best way' is debatable, I believe this is the best way for a few reasons:
- It works with multiple parts to an extension, eg
tar.gz
- Short and efficient code
- It works with both a filename and a complete path
3,20522 gold badges2626 silver badges4444 bronze badges
You can get all file extensions in a particular folder and do operations with a specific file extension:
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
Samir KarmacharyaSamir Karmacharya
FredFred
IMO, this is the best way if you have filenames like name.name.name.ext (ugly, but it sometimes happens):
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
Tommy89Tommy89
ltrim(strstr($file_url, '.'), '.')
this is the best way if you have filenames like name.name.name.ext (ugly, but it sometimes happens
Anjani BarnwalAnjani Barnwal
Sorry.. 'Short Question; But NOT Short Answer'
Example 1 for PATH
Example 2 for URL
Output of example 1:
Output of example 2:
References
AliAli
Use
for a quick extension retrieval (if you know for sure your file name has one).
AlexBAlexB
protected by Ashwini ChaudharyJul 6 '13 at 21:04
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged phpfile-extension or ask your own question.
Active5 months ago
I am designing a website. I want my website address to look like the following image:
I don't want my website to look like http://something.com/profile.phpI want
.php
extension to be removed in address bar when someone opens my website. In other words I want my website to be like: http://something.com/profileAs a second example, you can look at the StackOverflow website address itself.
Can someone please help me in getting this done? Thanks!
Vadim Kotov5,44577 gold badges3636 silver badges4949 bronze badges
Sumit GuptaSumit Gupta4,3112323 gold badges5959 silver badges7777 bronze badges
10 Answers
Just add .htaccess file to the root folder of your site(for example, /home/domains/domain.com/htdocs/) with following content:
IgorIgor
First, verify that the mod_rewrite module is installed. Then, be careful to understand how it works, many people get it backwards.
You don't hide urls or extensions. What you do is create a NEW url that directs to the old one, for example
The URL to put on your web site will be yoursite.com/play?m=asdf
or better yet
yoursite.com/asdf
Even though the directory asdf doesn't exist. Then with mod_rewrite installed you put this in .htaccess. Basically it says, if the requested URL is NOT a file and is NOT a directory, direct it to my script:
Almost done - now you just have to write some stuff into your PHP script to parse out the new URL. You want to do this so that the OLD ones work too - what you do is maintain a system by which the variable is always exactly the same OR create a database table that correlates the 'SEO friendly URL' with the product id. An example might be
/Some-Cool-Video (which equals product ID asdf)
The advantage to this? Search engines will index the keywords 'Some Cool Video.' asdf? Who's going to search for that?
I can't give you specifics of how to program this, but take the query string, strip off the end
turns into 'asdf'
Then set the m variable to this
m=asdf
So both URL's will still go to the same product
mod_rewrite can do lots of other important stuff too, Google for it and get it activated on your server (it's probably already installed.)
Shiv SinghShiv Singh4,46133 gold badges2727 silver badges3939 bronze badges
You have different choices.One on them is creating a folder named 'profile' and rename your 'profile.php' to 'default.php' and put it into 'profile' folder.and you can give orders to this page in this way:
Old page: http://something.com/profile.php?id=a&abc=1
New page: http://something.com/profile/?id=a&abc=1
If you are not satisfied leave a comment for complicated methods.
Php Get File Extension From Content-type
Iman HejaziIman Hejazi36122 gold badges77 silver badges1919 bronze badges
The problem with creating a directory and keeping index.php in it is that
- your links with menu will stop functioning
- There will be way too many directories. For eg, there will be a seperate directory for each and every question here on stackoverflow
The solutions are1. MOD REWRITE (as suggested above)2. use a php code to dynamically include other files in index file. Read a bit more abt it here http://inobscuro.com/tutorials/read/16/
Rahul SinghRahul Singh
Here is a simple PHP way that I use.
If a page is requested with the .php extension then a new request is made without the .php extension. The .php extension is then no longer shown in the browser's address field.
If a page is requested with the .php extension then a new request is made without the .php extension. The .php extension is then no longer shown in the browser's address field.
I came up with this solution because none of the many .htaccess suggestions worked for me and it was quicker to implement this in PHP than trying to find out why the .htaccess did not work on my server.
Put this at the beginning of each PHP file (preferrably before anything else):
Then put these functions in the file 'scripts.php':
Obviously you still need to have setup Apache to redirect any request without extension to the file with the extension.The above solution simply checks if the requested URI has an extension, if it does it requests the URI without the extension. Then Apache does the redirect to the file with the extension, but only the requested URI (without the extension) is shown in the browser's address field.The advantage is that all your 'href' links in your code can still have the full filename, i.e. including the .php extension.
AsgarAli1,98811 gold badge1515 silver badges3030 bronze badges
TonyTony
Actually, the simplest way to manipulate this is to
- Open a new folder on your server, e.g. 'Data'
- Put index.php (or index.html) in it
And then the URL www.yoursite.com/data will read that index.php file. If you want to take it further, open a subfolder (e.g. 'List') in it, put another index.php in that folder and you can have www.yoursite.com/data/list run that PHP file.
This way you can have full control over this, very useful for SEO.
Dan HorvatDan Horvat64022 gold badges1111 silver badges2525 bronze badges
JonJon5,46955 gold badges3232 silver badges4949 bronze badges
Remove a File Extenstion through .htaccess
Original URL: http://ravinderrathore.herobo.com/contact.php
htaccess rule to remove .php, .html etc. file extension from url.
Ravinder KumarRavinder Kumar
Tony, your script is ok, but if you have 100 files? Need add this code in all these :
include_once('scripts.php');
strip_php_extension();
strip_php_extension();
I think you include a menu in each php file (probably your menu is showed in all your web pages), so you can add these 2 lines of code only in your menu file. This work for me :D
user5499497
just nearly the same with the first answer about, but some more advantage.
Just add up if you have a other file-extension in your sites
Võ MinhVõ Minh