Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for validating folder name & file name

I want to validate a file name

Name of file or folder should not contain \ / ? % * : | " < > .

Could you please suggest me the regex expression to use in preg_match()?

Thanks.

like image 620
scoohh Avatar asked Jun 03 '11 01:06

scoohh


2 Answers

It would be more efficient to use the strpbrk() function.

if (strpbrk($filename, "\\/?%*:|\"<>") === FALSE) {
  /* $filename is legal; doesn't contain illegal character. */
}
else {
  /* $filename contains at least one illegal character. */
}
like image 51
King Skippus Avatar answered Sep 18 '22 12:09

King Skippus


The regex that meets your requirements: ^[^\\/?%*:|"<>\.]+$

like image 32
Alex Aza Avatar answered Sep 20 '22 12:09

Alex Aza