Solved

Finding last .

Posted on 2003-11-17
8
342 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

948 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

22 Experts available now in Live!

Get 1:1 Help Now