Get image extension
you can use image_type_to_extension
function with image type returned by getimagesize
:
$info = getimagesize($path);
$extension = image_type_to_extension($info[2]);
Get an image extension from an uploaded file in Laravel
You can use the pathinfo() function built into PHP for that:
$info = pathinfo(storage_path().'/uploads/categories/featured_image.jpg');
$ext = $info['extension'];
Or more concisely, you can pass an option get get it directly;
$ext = pathinfo(storage_path().'/uploads/categories/featured_image.jpg', PATHINFO_EXTENSION);
Get the file extension from images picked from gallery or camera, as string
filePath.substring(filePath.lastIndexOf(".")); // Extension with dot .jpg, .png
or
filePath.substring(filePath.lastIndexOf(".") + 1); // Without dot jpg, png
How to get the file extension in PHP?
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);
How to get Extension of image in angular?
You might use a regex using an alternation and the /i
for a case insensitive comparison or string compare using toLowerCase:
let extn = "TIFf";const regex = /(?:pdf|jpeg|jpg|tiff?|gif|png)/i;if (regex.test(extn)) { console.log(extn);}
let valid = [ "pdf", "tif", "tiff", "jpg", "jpeg", "gif", "png"];
if (valid.includes(extn.toLowerCase())) { console.log(extn);}
How can i get image name and image extension from image url
You can use pathinfo()
and parse_url()
:
$url = 'https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale';
// Getting the name
$name = pathinfo(parse_url($url)['path'], PATHINFO_FILENAME);
// Getting the extension
$ext = pathinfo(parse_url($url)['path'], PATHINFO_EXTENSION);
// Output:
var_dump($name); // 28vote_xp-articleLarge
var_dump($ext); // jpg
How can I get file extensions with JavaScript?
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.
Edit: In response to PhiLho's comment, use something like:
return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
Related Topics
How to Check If a Longitude/Latitude Point Is Within a Range of Coordinates
How to Decode a JSON String with Several Objects in PHP
When to Wrap Curly Braces Around a Variable
Using '$This' in an Anonymous Function in PHP Pre 5.4.0
Use PHP to Display MySQL Results in HTML Table
PHP Jquery Ajax Call Throws Net::Err_Empty_Response
.Htaccess Redirect Non-Www to Www Preserving Uri String
How to Loop Through JSON Array
Simulate File Structure with PHP
How to Install Imagemagick to Use with PHP on Windows 7 (3)
Add Column to Magento Admin Catolog > Manage Products
How to Merge Array and Preserve Keys
Stdclass Object and Foreach Loops