Solved

Finding last .

Posted on 2003-11-17
8
340 Views
Last Modified: 2008-03-06
What is the best way to find the last occurence of a . in a file name?

So like if my user submits My.File.zip 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.
0
Comment
Question by:drakkarnoir
  • 5
  • 2
8 Comments
 
LVL 2

Expert Comment

by:neele
ID: 9768715
u can use strrpos to get last occurance of string.

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

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

hope it helps
0
 
LVL 2

Accepted Solution

by:
neele earned 250 total points
ID: 9768744
i forget to say u can then use substr (Sub String)
e.g

$str = "funny.xyz";

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

will give u funny.zip

Hope it helps U
0
 

Author Comment

by:drakkarnoir
ID: 9768915
I'm trying to find the last occurence of a . not trying to concatnate them :\
0
 
LVL 20

Assisted Solution

by:Muhammad Wasif
Muhammad Wasif earned 250 total points
ID: 9769097
If u have the file name u can get the portion after the last . by

$local_file = "wasif.txt.tz";
$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

$_FILES['userfile']['type'][0]

WASIF
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Expert Comment

by:neele
ID: 9769152
SIGN ;c U din read my posting well ...

strrpos -> is FIND last occurance !!  STRRPOS

** Opps i put wrong just now lack a "R"
0
 
LVL 2

Expert Comment

by:neele
ID: 9769169
So once again U can
e.g.

$str = "hello.xxx.yyy.zzz";
//using
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. ...

0
 

Author Comment

by:drakkarnoir
ID: 9772073
Ah thank you very much, both of you. Sorry neele, I did not understand your solution but now I do :)
0
 
LVL 2

Expert Comment

by:neele
ID: 9775521
OK welcome :) maybe i din state well initially..
glad u get it, n solve ur issue. thanks for accepting the solution as well !
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now