I’m trying to get a images file extension and add some random number. Like 348678423.jpeg, 343344343.jpg, 32434343.gif, 3434343.png etc..
So following is my php code:
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
@$extension = getExtension($file);
$extension = strtolower($extension);
$image_named_uniq = uniqid().'.'.$extension;
But when i echo this variable $image_named_uniq it’s just show the random number, Like: 50aa5a2e0f425. It’s should be show 50aa5a2e0f425.jpg etc..
What is wrong in my code?
The answer posted by @jereon is correct, and the easiest way to do this.
However, if you would like to extract more information about the file without using explode and end, you can simply use the PHP built in
pathinfo()function.Reference: pathinfo()
This will return
As
$filenameis an array, you need to loop through it in order to read it intopathinfo(). This can be done very easily using a foreach loop like so: