Regular expression for check that an entered filename is valid
Regular expression for check that an entered filename is valid
am 18.12.2007 17:44:13 von bizt
Hi,
Im looking for a regular expression to check that a entered filename
is valid. I have a cms Im building where the user can enter the name
of a file, enter content and then click Save where the file is created
on the server. I need a regular expression to validate the filename
entered prior to this. I have looked on google where i found a couple
but they didnt work. Unfortuntely regular expression syntax is a very
foriegn langauge to me. Any help would be much appretiated. Thanks
Burnsy
Re: Regular expression for check that an entered filename is valid
am 18.12.2007 17:46:22 von Captain Paralytic
On 18 Dec, 16:44, bizt wrote:
> Hi,
>
> Im looking for a regular expression to check that a entered filename
> is valid. I have a cms Im building where the user can enter the name
> of a file, enter content and then click Save where the file is created
> on the server. I need a regular expression to validate the filename
> entered prior to this. I have looked on google where i found a couple
> but they didnt work. Unfortuntely regular expression syntax is a very
> foriegn langauge to me. Any help would be much appretiated. Thanks
>
> Burnsy
How do you define a valid filename?
Re: Regular expression for check that an entered filename is valid
am 19.12.2007 16:42:42 von Toby A Inkster
Captain Paralytic wrote:
> How do you define a valid filename?
Good question. As far as I know, for Linux and most UNIXy OSes it's this:
#[^/]#
i.e. any string that doesn't contain a slash is a valid filename. (Because
of course, the slash must be a directory separator.) Any other character
is valid in a file name -- asterisks, question marks, colons, tabs, line
feeds, whatever. Names "." and ".." are technically allowed, but they
always already exist.
It does depend on the filesystem though. Linux supports dozens of
different filesystems and some of those will limit you further.
Windows is a more fussy. As well as slashes, it doesn't allow colons,
backslashes, asterisks, question marks, whitespace other than the space
character itself, less-than and greater-than signs, double quotes and the
pipe symbol. There are also a small number of reserved file names that
cannot be created, such as "CON", "PRN", "CLOCK$" and "LPT1" -- these
special filenames were used to represent various hardware devices in DOS.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 12 days, 2:00.]
Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/11/28/itunes-sharing/
Re: Regular expression for check that an entered filename is valid
am 19.12.2007 17:51:47 von Steve
"Toby A Inkster" wrote in message
news:hoqn35-hv3.ln1@ophelia.g5n.co.uk...
> Captain Paralytic wrote:
>
>> How do you define a valid filename?
>
> Good question. As far as I know, for Linux and most UNIXy OSes it's this:
>
> #[^/]#
>
> i.e. any string that doesn't contain a slash is a valid filename. (Because
> of course, the slash must be a directory separator.) Any other character
> is valid in a file name -- asterisks, question marks, colons, tabs, line
> feeds, whatever. Names "." and ".." are technically allowed, but they
> always already exist.
>
> It does depend on the filesystem though. Linux supports dozens of
> different filesystems and some of those will limit you further.
>
> Windows is a more fussy. As well as slashes, it doesn't allow colons,
> backslashes, asterisks, question marks, whitespace other than the space
> character itself, less-than and greater-than signs, double quotes and the
> pipe symbol. There are also a small number of reserved file names that
> cannot be created, such as "CON", "PRN", "CLOCK$" and "LPT1" -- these
> special filenames were used to represent various hardware devices in DOS.
with the exception of CLOCK$ on win >= xp, you're exactly right...and i'm
glad i'm not the only one geeky enough to know that. :^)
Re: Regular expression for check that an entered filename is valid
am 19.12.2007 18:21:55 von colin.mckinnon
On 19 Dec, 16:51, "Steve" wrote:
> "Toby A Inkster" wrote in messagenews:hoqn35-hv3.ln1@ophelia.g5n.co.uk...
>
>
>
> > Captain Paralytic wrote:
>
> >> How do you define a valid filename?
>
> > Good question. As far as I know, for Linux and most UNIXy OSes it's this:
>
> > #[^/]#
>
> > i.e. any string that doesn't contain a slash is a valid filename. (Because
> > of course, the slash must be a directory separator.) Any other character
> > is valid in a file name -- asterisks, question marks, colons, tabs, line
> > feeds, whatever. Names "." and ".." are technically allowed, but they
> > always already exist.
>
> > It does depend on the filesystem though. Linux supports dozens of
> > different filesystems and some of those will limit you further.
>
> > Windows is a more fussy. As well as slashes, it doesn't allow colons,
> > backslashes, asterisks, question marks, whitespace other than the space
> > character itself, less-than and greater-than signs, double quotes and the
> > pipe symbol. There are also a small number of reserved file names that
> > cannot be created, such as "CON", "PRN", "CLOCK$" and "LPT1" -- these
> > special filenames were used to represent various hardware devices in DOS.
>
> with the exception of CLOCK$ on win >= xp, you're exactly right...and i'm
> glad i'm not the only one geeky enough to know that. :^)
Actually, you can create these files using some programming languages
- and get up to all sorts of mischief as a result (not least because
MS Windows doesn't let you delete them)
I'd go with is_writeable(realpath($filename)) (of course that doesn't
stop people writing files where they shouldn't.
C.
C.