Re: naming a directory after a user-submitted string

From: Date: Wed, 28 Jan 2004 07:50:28 +0000
Subject: Re: naming a directory after a user-submitted string
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-176019@lists.php.net to get a copy of this message
>Here's another question, possibly easier. Possibly even bone-headed. > >What kind of checking/filtering/changing do I need to do on a >user-submitted string before I can feel comfortable using it to name a >new directory in the web root on Linux/Apache? Anybody have a quick >Regular Expression they can toss at me? If so, I'd be muchly >appreciative. Or is this just a Terrible Idea That Should Never Be >Contemplated? A file or directory name in Unix can contain any character, except a slash. On mac OS, you also can't use a colon because that was the old mac way of delimiting directories. I imagine windows has a similar restriction on the backslash. I think it has to be less than 256 characters as well, but I may be remembering that incorrectly... Permissively, you could try: substr(preg_replace('/[\/\:\\]/', '_', $dirname), 0, 256) Though you may also want to be strict, and remove all non-word characters, i.e. letters, digits, slash and underscore: substr(preg_replace('/\W/', '_', $dirname), 0, 256) ...that will eliminate special case checks for ".", "..", and ".*". --------------------------------------------------------------------- michal migurski- contact info and pgp key: sf/ca http://mike.teczno.com/contact.html

« previous php.general (#176019) next »