Jump to content


preg_match question


3 replies to this topic

#1 Rainit

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 07 April 2008 - 09:25 AM

Im making a little gallery system. it takes pictures from a folder. Now i want to check if the files in the folder are image files. I have an array of allowed extensions

$allowed = array("jpg","gif",jpeg");

now i have extension

$ext = substr(strrchr($file, '.'), 1);
and i want to use preg_match or something to check if it is allowed extension. Now, do i use preg_match, preg_match_all or ereg? And what's the difference between them, i read the manual, but could find anythign except output. What i actually wanted to know was can i do it without a loop somehow. maybe the $allowed shouldnt be an array, a string of jpg OR gif OR jpeg. If its possible, then how can i pattern OR? I mean, what is the pattern for it.

Thanks for help! Hope You all understood.

#2 fycnrdths

    Young Padawan

  • Members
  • Pip
  • 16 posts
  • Location:stuck to my desk chair
  • Interests:programming.

Posted 07 April 2008 - 10:43 AM

View PostRainit, on Apr 7 2008, 09:25 AM, said:

$allowed = array("jpg","gif",jpeg");

...maybe the $allowed shouldnt be an array, a string of jpg OR gif OR jpeg. If its possible, then how can i pattern OR? I mean, what is the pattern for it.

Thanks for help! Hope You all understood.

I didn't really understand the last part. Are you worried that the array won't work? I have a similar system that does the same thing without the ereg, etc and it works just fine. If the extension of the file is not found in the array, then users receive an error and the file is not uploaded. Otherwise, it's uploaded.
 $filetype = array("txt", "zip");
  $type_status= 0;
  foreach($filetype as $ext) {
  if(substr($name, (0 - (strlen($ext)+1) )) == ".".$ext)
  $type_status= 1;}

Edited by fycnrdths, 07 April 2008 - 10:47 AM.


#3 Rainit

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 07 April 2008 - 11:47 AM

Sorry if you didn't understand, anyway i made it with foreach and preg_match. Why preg_match? Because of jpg and JPG for example. So i can use regex :P. (i for example ignores upper and lowercase.)

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 07 April 2008 - 05:22 PM

That's why god invented strcasecmp(), use that rather than == and you can avoid the overhead of preg_match() (just an option if you're concerned about efficiency).

Edited by rc69, 07 April 2008 - 05:23 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users