Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

if and or question

 
   Database Help (Home) -> PHP RSS
Next:  Stats for comp.lang.php  
Author Message
Jake Wiley

External


Since: Feb 12, 2005
Posts: 2



(Msg. 1) Posted: Sat Feb 12, 2005 7:06 pm
Post subject: if and or question
Archived from groups: comp>lang>php (more info?)

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.

 >> Stay informed about: if and or question 
Back to top
Login to vote
Ken Robinson1

External


Since: Sep 28, 2004
Posts: 4



(Msg. 2) Posted: Sat Feb 12, 2005 7:27 pm
Post subject: Re: if and or question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Jake Wiley wrote:
 > 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
 > }

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<!-- ~MESSAGE_AFTER~ -->

 >> Stay informed about: if and or question 
Back to top
Login to vote
Colin dot Horne at Gmail

External


Since: Feb 12, 2005
Posts: 1



(Msg. 3) Posted: Sat Feb 12, 2005 7:40 pm
Post subject: Re: if and or question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Also, you might consider using regular expressions:

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

Cheers
--Colin Horne
 >> Stay informed about: if and or question 
Back to top
Login to vote
Jake Wiley

External


Since: Feb 12, 2005
Posts: 2



(Msg. 4) Posted: Sat Feb 12, 2005 7:40 pm
Post subject: Re: if and or question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Ken Robinson wrote:
 > Jake Wiley wrote:
  > > 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
  > > }
 >
 > 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


Thank you sir Smile
It was right in front of me yet I couldn't see it. It's working now.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: if and or question 
Back to top
Login to vote
John Dunlop1

External


Since: May 20, 2004
Posts: 12



(Msg. 5) Posted: Sun Feb 13, 2005 4:40 am
Post subject: Re: if and or question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Colin dot Horne at Gmail dot com wrote:

 > Also, you might consider using regular expressions:
 >
 > if (preg_match("^audio/mp[e]?g$", $_FILES['userfile']['type']))

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<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: if and or question 
Back to top
Login to vote
Dave Patton1

External


Since: Sep 18, 2004
Posts: 19



(Msg. 6) Posted: Sun Feb 13, 2005 4:40 am
Post subject: Re: if and or question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Jake Wiley" <OEKilla.DeleteThis@msn.com> wrote in
news:1108260391.744238.99650@c13g2000cwb.googlegroups.com:

 > 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.

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
<a style='text-decoration: underline;' href="http://www.confluence.org/" target="_blank">http://www.confluence.org/</a>
My website: <a style='text-decoration: underline;' href="http://members.shaw.ca/davepatton/" target="_blank">http://members.shaw.ca/davepatton/</a><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: if and or question 
Back to top
Login to vote
Colin Horne

External


Since: Feb 12, 2005
Posts: 2



(Msg. 7) Posted: Mon Feb 14, 2005 12:39 pm
Post subject: Re: if and or question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: if and or question 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
The "It doesn't even qualify as a newbie question" question - I'm sure all of you will get a laugh out of this, but I'm perfectly willing to reveal how little I know. I've been tearing my hair out looking for a program that will rename files in a folder on my hard drive with totally random names. Nothing I..

question about safe - question no. 2 - Hello! How to prevent from such try of attack of the website? http://www.domain.com/index.php?id=%3Cscript%3Ealert(document.cookie);%3C/script%3E Thank you in advance for help M.

question on name - How do I put a ' in someones name list John's House. In a php file it breaks the code.

question about NG popularity - Does anybody know why the Pyhon Newsgroup is so much more popular than both of the php newsgroups? My impression was that PHP was wildly popular - so I do not understand the seeming discrepency here? Python NG is over 13,000, while PHP is about 3,000...

newbie question - Sorry if missed something obvious. I'm trying to write a notebook program that allows me to select a "subject" from a pulldown menu and read the corresponding "data" back into a form where I can edit and save it. The MySQL database t...
   Database Help (Home) -> PHP All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]