Connecting Tech Pros Worldwide Forums | Help | Site Map

if and or question

Jake Wiley
Guest
 
Posts: n/a
#1: Jul 17 '05
I'm trying to create an mp3 upload page but noticed that the mime type
on my laptop is audio/mp3 while on my desktop it is audio/mpeg. I want
to allow both types to upload.
I have tried this:
if (($_FILES['userfile']['type'] != 'audio/mp3') ||
($_FILES['userfile']['type'] != 'audio/mpeg'))
{
echo an error message
then exit the program
}

I tried several variations of this but as soon as it finds that it
doesn't meet the first criteria it aborts even though it is of
audio/mpeg type.


Ken Robinson
Guest
 
Posts: n/a
#2: Jul 17 '05

re: if and or question



Jake Wiley wrote:[color=blue]
> I'm trying to create an mp3 upload page but noticed that the mime[/color]
type[color=blue]
> on my laptop is audio/mp3 while on my desktop it is audio/mpeg. I[/color]
want[color=blue]
> to allow both types to upload.
> I have tried this:
> if (($_FILES['userfile']['type'] != 'audio/mp3') ||
> ($_FILES['userfile']['type'] != 'audio/mpeg'))
> {
> echo an error message
> then exit the program
> }[/color]

Think about what you're saying here. In english:

if the filetype is not equal 'audio/mp3' or the file type is not equal
'audio/mpeg' then not good.

So that says to exit on error if either condition is met. What you want
is to exit out if both conditions are not met. In english:

if the filetype is not equal 'audio/mp3' and the file type is not equal
'audio/mpeg' then not good.

or in PHP
if (($_FILES['userfile']['type'] != 'audio/mp3') &&
($_FILES['userfile']['type'] != 'audio/mpeg'))

It is usually helpful to say it outload to see what's happening.

Ken

Colin dot Horne at Gmail dot com
Guest
 
Posts: n/a
#3: Jul 17 '05

re: if and or question


Also, you might consider using regular expressions:

if (preg_match("^audio/mp[e]?g$", $_FILES['userfile']['type']))
{ ... }
else
{ ... }

Cheers
--Colin Horne

Jake Wiley
Guest
 
Posts: n/a
#4: Jul 17 '05

re: if and or question



Ken Robinson wrote:[color=blue]
> Jake Wiley wrote:[color=green]
> > I'm trying to create an mp3 upload page but noticed that the mime[/color]
> type[color=green]
> > on my laptop is audio/mp3 while on my desktop it is audio/mpeg. I[/color]
> want[color=green]
> > to allow both types to upload.
> > I have tried this:
> > if (($_FILES['userfile']['type'] != 'audio/mp3') ||
> > ($_FILES['userfile']['type'] != 'audio/mpeg'))
> > {
> > echo an error message
> > then exit the program
> > }[/color]
>
> Think about what you're saying here. In english:
>
> if the filetype is not equal 'audio/mp3' or the file type is not[/color]
equal[color=blue]
> 'audio/mpeg' then not good.
>
> So that says to exit on error if either condition is met. What you[/color]
want[color=blue]
> is to exit out if both conditions are not met. In english:
>
> if the filetype is not equal 'audio/mp3' and the file type is not[/color]
equal[color=blue]
> 'audio/mpeg' then not good.
>
> or in PHP
> if (($_FILES['userfile']['type'] != 'audio/mp3') &&
> ($_FILES['userfile']['type'] != 'audio/mpeg'))
>
> It is usually helpful to say it outload to see what's happening.
>
> Ken[/color]


Thank you sir :-)
It was right in front of me yet I couldn't see it. It's working now.

Dave Patton
Guest
 
Posts: n/a
#5: Jul 17 '05

re: if and or question


"Jake Wiley" <OEKilla@msn.com> wrote in
news:1108260391.744238.99650@c13g2000cwb.googlegro ups.com:
[color=blue]
> I'm trying to create an mp3 upload page but noticed that the mime type
> on my laptop is audio/mp3 while on my desktop it is audio/mpeg. I want
> to allow both types to upload.
> I have tried this:
> if (($_FILES['userfile']['type'] != 'audio/mp3') ||
> ($_FILES['userfile']['type'] != 'audio/mpeg'))
> {
> echo an error message
> then exit the program
> }
>
> I tried several variations of this but as soon as it finds that it
> doesn't meet the first criteria it aborts even though it is of
> audio/mpeg type.[/color]

Once you fix the logic error(OR vs AND), you should consider
that the above logic may not be robust enough:
- the client(the browser) may not provide the mime type
- the client may provide a different mime type, even though
the file is an mp3 file
- the client may be spoofing the mime type

--
Dave Patton
Canadian Coordinator, Degree Confluence Project
http://www.confluence.org/
My website: http://members.shaw.ca/davepatton/
John Dunlop
Guest
 
Posts: n/a
#6: Jul 17 '05

re: if and or question


Colin dot Horne at Gmail dot com wrote:
[color=blue]
> Also, you might consider using regular expressions:
>
> if (preg_match("^audio/mp[e]?g$", $_FILES['userfile']['type']))[/color]

A couple of points:

Remember delimiters. Just now you'll get a warning that
there is not an ending delimiter. '^', if uncommon, is fine
as a delimiter. I tend to use backticks, however, because
they seldom appear in the subjects of my patterns.

The character class is superfluous. The patterns
`^audio/mpe?g$` and `^audio/mp[e]?g$` are equivalent.

Your pattern allows for the strings 'audio/mpeg' and
'audio/mpg' but not 'audio/mp3'. Consider the slightly more
complicated `^audio/mp(?:3|e?g)$` (or `^audio/mp(?:e?g|3)$`
if you think the mpg and mpeg subtypes are more likely).

HAGS!

--
Jock
Colin Horne
Guest
 
Posts: n/a
#7: Jul 17 '05

re: if and or question


Sorry, my mistake - I briefly debugged it on my system, and forgot to
re-copy and paste the code to the reply.

"/audio\/mp[e]?g$/i" was my final pattern, had I remembered to copy it
back in.

Good point with regards to the brackets around the 'e'.

Cheers
--Colin

Closed Thread


Similar PHP bytes