Jump to content

Recommended Posts

UNIX filenames or Directory name can contain anything other than a / Character.

I dont have Windows I use Linux

In windows

Create a new file and then try renaminf it to a String that contains a / when you press that Windows will show you which characters are not Allowed

i have string. how do i check if it only contains numbers, aplhabtes, . - and _

 

Use preg_match.

 

UNIX filenames or Directory name can contain anything other than a / Character.

 

But I wouldn't recommend using anything other than what Ive suggested. Special chars can wreak havoc with shell expansion.

I think you should take thorpe's advice and only allow letters, numbers, underscore, minus and dot.

 

If that is the case a regex to test a filename would look like this:

 

preg_match("/([a-z0-9\._-])+/i",$filename)

 

 

And for the path:

preg_match("/([a-z0-9\.\\\/_-])+/",$path)

 

I'm not 100% sure if / (slash) is a special character and needs escaping by \ (backslash). So the "\\\/" part of the regex is first escaping a backslash "\\" and then escaping a slash "\/" but notice that I wasn't sure if / (slash) needed escaping. Furthermore I don't know if you want both / and \ to be allowed in the path...

 

And if the two code pieces don't speak for them selves I would recommend you to split up the path in "path" and "filename" :)

 

 

By the way this is a very useful regex site: http://www.regular-expressions.info/reference.html

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.