determine if file name has 2 dots in it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

determine if file name has 2 dots in it

Post by Smudly »

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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: determine if file name has 2 dots in it

Post by requinix »

For simplicity's sake, I'd use substr_count.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: determine if file name has 2 dots in it

Post by McInfo »

Why would it matter if a file name has two dots? If you're worried about the content of a file, check the content, not the file name. Disallowing "configure.php.jpg" but allowing "configure.jpg" does not prevent someone from uploading a file containing PHP. If you want files named a certain way, rename them. Why cause trouble for users who want to upload files with names like "version_1.0.gif", "date_2010.10.11.png", or "forums.devnetwork.net.jpg"?
Post Reply