2 Replies - 9961 Views - Last Post: 09 November 2008 - 11:40 PM Rate Topic: -----

#1 rafid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-October 07

regular expression to find file extension

Posted 09 November 2008 - 09:27 PM

I am trying to work a page where we have a form:
user enters file information and clicks upload.
the information is passed to another page where we check the file type we passed as a session variable, and then i want to check whether the file ends with .mp3 or .wav or not.

My problem is, i am trying to get the file extension using a regular expression and pattern matching. But this is my first time using PHP and i am not sure how pattern matching works here. I need your help please.

if($_SESSION['fileType'] == 'audio')
{
$pattern = '/[(.mp3)(.wav)]$/'; 

	if(!preg_match($pattern, $fileName))
	{
		   echo "error: file must be mp3 or wav";
	}
		else
		{
				/** Upload script goes here **/
		}
}



I get the following error:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

Is This A Good Question/Topic? 0
  • +

Replies To: regular expression to find file extension

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5077
  • View blog
  • Posts: 13,705
  • Joined: 18-April 07

Re: regular expression to find file extension

Posted 09 November 2008 - 11:17 PM

Well you are using it properly here and the tests I have ran on it appear to be ok. Was there a test earlier where you had no / / on the pattern? This typically causes the described warning problem. They require the forward slashes. If you had forgotten those earlier, then perhaps you are looking at a cached version of the page.

Otherwise what you have shown there is perfectly valid and should be proper given that $fileName actually equals a path.

Are you sure that the code you are showing is the problem? Could it be another pattern somewhere?

:)
Was This Post Helpful? 0
  • +
  • -

#3 rafid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 21-October 07

Re: regular expression to find file extension

Posted 09 November 2008 - 11:40 PM

View PostMartyr2, on 9 Nov, 2008 - 10:17 PM, said:

Well you are using it properly here and the tests I have ran on it appear to be ok. Was there a test earlier where you had no / / on the pattern? This typically causes the described warning problem. They require the forward slashes. If you had forgotten those earlier, then perhaps you are looking at a cached version of the page.

Otherwise what you have shown there is perfectly valid and should be proper given that $fileName actually equals a path.

Are you sure that the code you are showing is the problem? Could it be another pattern somewhere?

:)



Wow, you're right. It looks like I tried changing it but forgot to test it and then posted here lol. My bad.

Appreciate the reply though, thanks a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1