Connecting Tech Pros Worldwide Help | Site Map

if and or question

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 11:28 AM
Jake Wiley
Guest
 
Posts: n/a
Default if and or question

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.


  #2  
Old July 17th, 2005, 11:28 AM
Ken Robinson
Guest
 
Posts: n/a
Default 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

  #3  
Old July 17th, 2005, 11:28 AM
Colin dot Horne at Gmail dot com
Guest
 
Posts: n/a
Default 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

  #4  
Old July 17th, 2005, 11:29 AM
Jake Wiley
Guest
 
Posts: n/a
Default 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.

  #5  
Old July 17th, 2005, 11:29 AM
Dave Patton
Guest
 
Posts: n/a
Default 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/
  #6  
Old July 17th, 2005, 11:29 AM
John Dunlop
Guest
 
Posts: n/a
Default 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
  #7  
Old July 17th, 2005, 11:31 AM
Colin Horne
Guest
 
Posts: n/a
Default 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

 

Bookmarks

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.