Finding last .

What is the best way to find the last occurence of a . in a file name?

So like if my user submits how would I find the . right before the zip?

The reason I'm asking (maybe there is a different answer to this) is because I want to be able to accept odd file types that aren't defined in PHP like models, .mdl. But PHP identifies this is as application/octet-stream along with any other file types it doesn't understand.

Thanks in advance.
Who is Participating?
i forget to say u can then use substr (Sub String)

$str = "";

$newFilename = substr($str, 0, strpos ($str, ".")).".zip"; //start from 0 end with 8

will give u

Hope it helps U
u can use strrpos to get last occurance of string.

$str = "morning";
echo strpos($str, "m"); //answer is 0

$str = "morning morning";
echo strpos($str, "m"); //answer is 8

hope it helps
drakkarnoirAuthor Commented:
I'm trying to find the last occurence of a . not trying to concatnate them :\
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Muhammad WasifCommented:
If u have the file name u can get the portion after the last . by

$local_file = "";
$ext_array =explode(".",$local_file);
$last_position = count($ext_array) - 1  ;
$extension = $ext_array[$last_position] ;

echo $extension;

it will return  tz

and u r geting the uploaded files in array userfile[]  u can get the filetype by


SIGN ;c U din read my posting well ...

strrpos -> is FIND last occurance !!  STRRPOS

** Opps i put wrong just now lack a "R"
So once again U can

$str = "";
strrpos($str, ".");  //GIVE u the INDEX of LAST occurance ! in this case is 13

I give u the concatnate portion is to help U with the coding in replacing the last e.g. .zzz with your appropriate extension. Thought it might help U. ...

drakkarnoirAuthor Commented:
Ah thank you very much, both of you. Sorry neele, I did not understand your solution but now I do :)
OK welcome :) maybe i din state well initially..
glad u get it, n solve ur issue. thanks for accepting the solution as well !
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.