Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
determine if file name has 2 dots in it
Old 10-10-2010, 09:57 PM determine if file name has 2 dots in it
Skilled Talker

Posts: 83
Trades: 0
My uploader allows the following file formats:

jpg
pdf
gif
png

I tested that if someone was to name a file configure.php.jpg, my uploader allows it to upload

I want to do a check to see if the user has 2 extensions, and if so it will not allow them to upload.

I was thinking of just checking if the file name has two "." (dots) in the name. What function could I use to do this?

Or .. is there a better way?
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-10-2010, 10:44 PM Re: determine if file name has 2 dots in it
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
substr_count

I'm not sure why you want to disallow files with '2 extensions'. configure.php.jpg is a perfectly valid name for a jpg.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-11-2010, 04:15 AM Re: determine if file name has 2 dots in it
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
If someone upload a file with name like that, he cant do any harm unless he rename the file and for that he needs access to your web server.
miki86 is online now
Reply With Quote
View Public Profile
 
Old 10-11-2010, 03:00 PM Re: determine if file name has 2 dots in it
3nomore's Avatar
Experienced Talker

Posts: 40
Name: Felipe
Trades: 0
I agree, work on the final extension only and that should be enough
3nomore is offline
Reply With Quote
View Public Profile Visit 3nomore's homepage!
 
Old 10-11-2010, 07:19 PM Re: determine if file name has 2 dots in it
Skilled Talker

Posts: 52
Name: Alex
Trades: 0
if you want to disallow only ".php" inside filename, you can check it with
strpos(strtolower($file_name),'.php')
or
EREGI("\.php",$file_name)
so,
if(!strpos(strtolower($file_name),'.php')){...allo w}
elf2002 is offline
Reply With Quote
View Public Profile
 
Old 10-13-2010, 06:10 PM Re: determine if file name has 2 dots in it
Novice Talker

Posts: 8
Trades: 0
Get the real file extension with this:

$realExtn = substr($filename,-3);

switch($realExtn)
{
case 'jpg':
case 'pdf':
case 'gif':
case 'png':

print "OK";
break;

default:

print "Bad";
}
Jsecure is offline
Reply With Quote
View Public Profile
 
Old 10-13-2010, 06:39 PM Re: determine if file name has 2 dots in it
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Jsecure View Post
Get the real file extension with this:

$realExtn = substr($filename,-3);

switch($realExtn)
{
case 'jpg':
case 'pdf':
case 'gif':
case 'png':

print "OK";
break;

default:

print "Bad";
}
This assumes that an extensions is only 3 characters. It also does not account for case (the extension JPG would be considered invalid). Here is a better way:
PHP Code:
function getExtension($str
{
    
//get all of the characters following the last .
    
$ext substr(strrchr($str'.'), 1);

    
//make it safe to assume the extension is lower case
    
return strtolower($ext);

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to determine if file name has 2 dots in it
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.02159 seconds with 12 queries