Re: Re: validating directory and file name with preg_match
| From: | Frank Stanovcak | Date: | Wed, 28 Jan 2009 19:14:27 +0000 |
| Subject: | Re: Re: validating directory and file name with preg_match | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-287285@lists.php.net to get a copy of this message | ||
""Boyd, Todd M."" <tmboyd1@ccis.edu> wrote in message
news:33BDE0B2C17EEF46ACBE00537CF2A19003BB9843@EXCHCLUSTER.ccis.edu...
> -----Original Message-----
> From: Frank Stanovcak [mailto:blindspotpro@comcast.net]
> Sent: Wednesday, January 28, 2009 1:04 PM
> To: php-general@lists.php.net
> Subject: [PHP] Re: validating directory and file name with preg_match
>
>
> ""Frank Stanovcak"" <blindspotpro@comcast.net> wrote in message
> news:A8.D6.08436.5CF80894@pb1.pair.com...
> > 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
> > };
> >
>
> ------Then Robyn Sed------
> Looks like the difference between the 2 patterns is the
> '(entry|edit)\\\\' part -- couldn't you make that optional and combine
> it into one expression? Ie, '((entry|edit)\\\\)*'
>
> As for returning the filename, basename() is your friend.
>
> R
> ------end------
>
> I would use basename, but I want only the two file types specified to
> trigger a return. They are the only two valid php types on this
> server.
>
> Your suggestion worked like a charm! Thank you very much!
Upon reading this, I realize that I over-thought my suggestion.
/^c:\\inetpub\\wwwroot\\folder\\(?:entry|edit)\\(\w+\.(?:php|pdf))$/i
That's my final answer. :)
// Todd
Thank you too Todd. I don't know why, but for some reason if I don't double
escape the slashes it fails everytime. I never knew you could stop the
return with ?: that will help a great deal in the future!
Frank