Re: validating directory and file name with preg_match

From: Date: Wed, 28 Jan 2009 18:35:37 +0000
Subject: Re: validating directory and file name with preg_match
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-287279@lists.php.net to get a copy of this message
Frank Stanovcak wrote:
I'm limiting access to certain proceedures based on the file trying to use them, and the directory they are located in on my server. Right now I am using two preg_match statments as you will see. What I want to know is this. Is there a way to write a single regex for this that will supply the file name as a match, and only return if the directory is valid? ------------ //make sure we are calling from the propper directory, and get the file name that included to determine //database access needs preg_match('#^C:\\\\Inetpub\\\\wwwroot\\\\folder\\\\(entry|edit)\\\\(\w*\\.(php|pdf))#i', $included_files[0], $check1); preg_match('#^C:\\\\Inetpub\\\\wwwroot\\\\folder\\\\(\w*\\.(php|pdf))#i', $included_files[0], $check2); if(isset($check1)){ if(is_array($check1)){ $matches[4] = $check1[2]; }; unset($check1); }; if(isset($check2)){ if(is_array($check2)){ $matches[4] = $check2[1]; }; unset($check2); }; if(isset($matches[4]){ more code here }; I don't have time to go thru your code in detail; but, here are a couple of hints:
Get rid of all those "\"s Use the hex equivalents for special characters. e.g. "." use \x2E It's tough enough testing regex expressions without worrying about all the back slashes. You need preg_match_all() Consider: if(!preg_match_all()){//no match code here}//Or maybe do the opposite

« previous php.general (#287279) next »