How to get the file extension in PHP? [duplicate]
No need to use string functions. You can use something that’s actually designed for what you want: pathinfo(): $path = $_FILES[‘image’][‘name’]; $ext = pathinfo($path, PATHINFO_EXTENSION);
No need to use string functions. You can use something that’s actually designed for what you want: pathinfo(): $path = $_FILES[‘image’][‘name’]; $ext = pathinfo($path, PATHINFO_EXTENSION);
Newer Edit: Lots of things have changed since this question was initially posted – there’s a lot of really good information in wallacer’s revised answer as well as VisioN’s excellent breakdown Edit: Just because this is the accepted answer; wallacer’s answer is indeed much better: return filename.split(‘.’).pop(); My old answer: return /[^.]+$/.exec(filename); Should do it. … Read more
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 :-)). In fact, it does exist, but few people know it. Meet pathinfo(): $ext = pathinfo($filename, PATHINFO_EXTENSION); This is fast and built-in. pathinfo() can give you … Read more
Markdown is a plain-text file format. The extensions .md and .markdown are just text files written in Markdown syntax. If you have a Readme.md in your repo, GitHub will show the contents on the home page of your repo. Read the documentation: Standard Markdown GitHub Flavored Markdown You can edit the Readme.md file in GitHub … Read more
Use os.path.splitext: >>> import os >>> filename, file_extension = os.path.splitext(‘/path/to/somefile.ext’) >>> filename ‘/path/to/somefile’ >>> file_extension ‘.ext’ Unlike most manual string-splitting attempts, os.path.splitext will correctly treat /a/b.c/d as having no extension instead of having extension .c/d, and it will treat .bashrc as having no extension instead of having extension .bashrc: >>> os.path.splitext(‘/a/b.c/d’) (‘/a/b.c/d’, ”) >>> os.path.splitext(‘.bashrc’) … Read more