473,383 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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.

Jul 17 '05 #1
6 1793

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

Jul 17 '05 #2
Also, you might consider using regular expressions:

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

Cheers
--Colin Horne

Jul 17 '05 #3

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 :-)
It was right in front of me yet I couldn't see it. It's working now.

Jul 17 '05 #4
"Jake Wiley" <OE*****@msn.com> wrote in
news:11*********************@c13g2000cwb.googlegro ups.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
http://www.confluence.org/
My website: http://members.shaw.ca/davepatton/
Jul 17 '05 #5
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
Jul 17 '05 #6
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

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.