By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Regular expression for check that an entered filename is valid

P: n/a
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
Dec 18 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 18 Dec, 16:44, bizt <bissa...@yahoo.co.ukwrote:
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?
Dec 18 '07 #2

P: n/a
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/1...tunes-sharing/
Dec 19 '07 #3

P: n/a

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:ho************@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. :^)
Dec 19 '07 #4

P: n/a
On 19 Dec, 16:51, "Steve" <no....@example.comwrote:
"Toby A Inkster" <usenet200...@tobyinkster.co.ukwrote in messagenews:ho************@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.
Dec 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.