/    Sign up×
Community /Pin to ProfileBookmark

Remove non-file/folder characters

What type of characters can’t be in a file or folder name?

How can I remove them after submitted?

I feel that there is a better way than this:

[code=php]$val = array(“?”,”!”,”.”,”@”,”#”,”$”,”%”,”^”,”*”,”:”);
$rep = “”;
$string = “Some random String !@#$%^*”;

$nedit = str_replace($val, $rep, $string);[/code]

I don’t want to do that, because I’m sure I missed so many characters there.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordOct 05.2006 — I know you posted in php and you might convert this into php (If you do please post) but its javascript.

I ran into the same problem, users uploading files with all sorts of wierd names, so I just validate before they upload now. works for me, but I know they can turn Javascript off, so looking for the php equivalent

[CODE]
function valid_file_name()
{
var tsendto = document.getElementById('userfile').value;
var sarray = new Array();
sarray = tsendto.split('\');
the_name = sarray.length;
fill = ' '; //Used to space out the alert box

var FName = sarray[the_name - 1];
var isFilenameLegal = /^[a-zA-Z0-9_-]*.[a-zA-Z0-9]*$/.test(FName,"g");
if(!isFilenameLegal)
{
alert('INVALID FILENAME: nn"' + sarray[the_name - 1] + '"nnPlease Click the "Need Help with files" link or visit the "Help" section of this sitento find more information on file naming specifications' + fill);
return false;
}
else
{
//alert('Valid filename');
return true;
}

}

[/CODE]


hope this helps
Copy linkTweet thisAlerts:
@netbuddyOct 05.2006 — If they turn off JS, your site HTML code should be able to use the HTML tags <noscript> ... </noscript>

Never used them myself but they exist.
Copy linkTweet thisAlerts:
@Reli4ntOct 06.2006 — rather than looking for the characters to prevent, you could use a little regex to permit only alphanumeric [code=php]"^[A-Za-z0-9]+$"[/code]Maybe add in an underscore or a dash too. I dont think spaces should be permitted though.
Copy linkTweet thisAlerts:
@netbuddyOct 06.2006 — Or even better, substitute those invalid characters for ones that are...

The invalid characters are:
/ : * ? " < > | [/quote]

While doulbe quotes are not permitted, single quotes are... go figure.
Copy linkTweet thisAlerts:
@Reli4ntOct 06.2006 — I thought tildas (~), backticks (`), @, etc. were problematic for some platforms.
Copy linkTweet thisAlerts:
@NogDogOct 06.2006 — What is valid/invalid depends on the OS. But generally speaking, letters, underscores, digits, and periods (full stops) are always valid. In the past what I've done is replace any other charactrer with an underscore:
[code=php]
$filename = preg_replace('/[^w.]/', '_', $filename);
[/code]

'w' = [a-zA-Z0-9_]
×

Success!

Help @The_Little_Guy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...