By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,939 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,939 IT Pros & Developers. It's quick & easy.

preg_match explanation

P: n/a
Hello
Found this piece of code using preg_match to check file types during upload
of files.
$allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
preg_match("/\." . $allowed_file_types . "$/i",
$_FILES['uploadedfile']["name"])

I understand the basic preg_match but am confused as to how the string
pattern part is working i.e.
"/\." . $allowed_file_types . "$/i"

I need to understand it better as although it appears to work in other parts
of my site, Im getting an error message on one page

Warning: preg_match() expects parameter 2 to be string, array given in
/home/iddsoftw/public_html/questiondbase/usersquestions.php on line 52

Can anyone give a step by step explanation of what its doing
Thanks
Ian
Oct 15 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Rik
mantrid wrote:
Hello
Found this piece of code using preg_match to check file types during
upload of files.
No, after uploading, unfortunately.
$allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
preg_match("/\." . $allowed_file_types . "$/i",
$_FILES['uploadedfile']["name"])

I understand the basic preg_match but am confused as to how the string
pattern part is working i.e.
"/\." . $allowed_file_types . "$/i"

I need to understand it better as although it appears to work in
other parts of my site, Im getting an error message on one page

Warning: preg_match() expects parameter 2 to be string, array given in
/home/iddsoftw/public_html/questiondbase/usersquestions.php on line 52

Can anyone give a step by step explanation of what its doing

Your problem is not in the regex (which means: filename has to end on
..jpg/.jpeg etc.).
Your problem is in the $_FILES part.
If you upload 1 file, named uploadedfile, $_FILES['uploadedfile']['name']
contains the string with the name of that file. If however you upload more
files (several inputs named uploadedfile[] for instance),
$_FILES['uploadedfile']['name'] will contain an array of names of the
uploaded file. That array won't fit in preg_match, and you'll have to loop
in some way through the $_FILES array.

var_dump($_FILES) in the bugging script, and all will be clear I think.

Grtz,
--
Rik Wasmus
Oct 15 '06 #2

P: n/a
Hi

silly me, I am using a loop and I see now that I left the [$i] of the end of
['uploadedfile']["name"] in the preg_match. It works now, but I'd still like
to know how the pattern variable is working in the code.

Ian

"Rik" <lu************@hotmail.comwrote in message
news:55***************************@news1.tudelft.n l...
mantrid wrote:
Hello
Found this piece of code using preg_match to check file types during
upload of files.

No, after uploading, unfortunately.
$allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
preg_match("/\." . $allowed_file_types . "$/i",
$_FILES['uploadedfile']["name"])

I understand the basic preg_match but am confused as to how the string
pattern part is working i.e.
"/\." . $allowed_file_types . "$/i"

I need to understand it better as although it appears to work in
other parts of my site, Im getting an error message on one page

Warning: preg_match() expects parameter 2 to be string, array given in
/home/iddsoftw/public_html/questiondbase/usersquestions.php on line 52

Can anyone give a step by step explanation of what its doing


Your problem is not in the regex (which means: filename has to end on
.jpg/.jpeg etc.).
Your problem is in the $_FILES part.
If you upload 1 file, named uploadedfile, $_FILES['uploadedfile']['name']
contains the string with the name of that file. If however you upload more
files (several inputs named uploadedfile[] for instance),
$_FILES['uploadedfile']['name'] will contain an array of names of the
uploaded file. That array won't fit in preg_match, and you'll have to loop
in some way through the $_FILES array.

var_dump($_FILES) in the bugging script, and all will be clear I think.

Grtz,
--
Rik Wasmus


Oct 15 '06 #3

P: n/a
Found this piece of code using preg_match to check file types during
upload
of files.
$allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
preg_match("/\." . $allowed_file_types . "$/i",
$_FILES['uploadedfile']["name"])

I understand the basic preg_match but am confused as to how the string
pattern part is working i.e.
"/\." . $allowed_file_types . "$/i"
the regular expression equates to the following:-

"/\.(jpg|jpeg|gif|bmp|png)$/i"

basically it is saying that the string MUST contain a . (which has been
escaped by the slash) plus jpg OR jpeg OR gif OR bmp OR png. AS you see the
| means OR when enclosed in brackets like it currently is. The $ states that
the match must be at the end of the string (or in this case the file name)
and the /i staes that the match should be case insensitive.

Oct 16 '06 #4

P: n/a
thanks that explains a lot.
if i wanted to make it case insensitive should I simply remove the /i ?
also I thought || was 'or', is this special only to preg_match?
also
why the /\. and not just \.

Ian
"peter" <su****@flexiwebhost.comwrote in message
news:eg**********@emma.aioe.org...
>
Found this piece of code using preg_match to check file types during
upload
of files.
$allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
preg_match("/\." . $allowed_file_types . "$/i",
$_FILES['uploadedfile']["name"])

I understand the basic preg_match but am confused as to how the string
pattern part is working i.e.
"/\." . $allowed_file_types . "$/i"

the regular expression equates to the following:-

"/\.(jpg|jpeg|gif|bmp|png)$/i"

basically it is saying that the string MUST contain a . (which has been
escaped by the slash) plus jpg OR jpeg OR gif OR bmp OR png. AS you see
the
| means OR when enclosed in brackets like it currently is. The $ states
that
the match must be at the end of the string (or in this case the file name)
and the /i staes that the match should be case insensitive.

Oct 16 '06 #5

P: n/a
mantrid wrote:
>
"peter" <su****@flexiwebhost.comwrote in message
news:eg**********@emma.aioe.org...
>>Found this piece of code using preg_match to check file types during
upload
of files.
$allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
preg_match("/\." . $allowed_file_types . "$/i",
$_FILES['uploadedfile']["name"])

I understand the basic preg_match but am confused as to how the string
pattern part is working i.e.
"/\." . $allowed_file_types . "$/i"
the regular expression equates to the following:-

"/\.(jpg|jpeg|gif|bmp|png)$/i"

basically it is saying that the string MUST contain a . (which has been
escaped by the slash) plus jpg OR jpeg OR gif OR bmp OR png. AS you see
the
>| means OR when enclosed in brackets like it currently is. The $ states
that
>the match must be at the end of the string (or in this case the file name)
and the /i staes that the match should be case insensitive.

(top posting corrected)
thanks that explains a lot.
if i wanted to make it case insensitive should I simply remove the /i ?
also I thought || was 'or', is this special only to preg_match?
also
why the /\. and not just \.

Ian
Many languages distinguish '|' bitwise OR from '||' boolean OR. It had
never occurred to me before that '|' in regexp had any connection with
these operators: I guess therefore that the answer is that there is no
need to distinguish two different kinds of OR so you might as well use a
single character.

The / .. / pair are the delimiters of the pattern. I think you can use
any character that doesn't occur in the pattern (and I suspect that, as
in Perl, you can use matched characters like '{' and '}' if they don't
occur in the pattern, though I can't quickly find a reference to say so).

We talk of the modifier as being '/i', but really it's just 'i'
immediately following the closing delimiter.

Colin
Oct 16 '06 #6

P: n/a
thanks that explains a lot.
if i wanted to make it case insensitive should I simply remove the /i ?
also I thought || was 'or', is this special only to preg_match?
also
why the /\. and not just \.
as colin pointed out the / are in this case signifying the start and end of
the pattern you could have quite easily for example used & which I have done
in the past when it seemed more appropriate. the \ is actually escaping the
.. as it has special meaning (a . matches any single character and of course
... matches 2 characters).

1 thing to watch with regular expressions is that certain characters have 1
meaning in 1 situation and another meaning ina different situation for
example ^ at the start of a regular expression means to match at the start
of a string (or new line depending on the mode you are running the regular
expression and of course which language the regular expression is used in)
but it also means anything but in a character set for example the following
would match anything BUT a w [^w].
Oct 17 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.