Reject If File Name Contains Non English Character
Solution 1:
Get the value of the file
input, and match it against the regex \w
But you should not disallow this. You should instead rename the file after it is uploaded. The user may upload a file with a name valid in his OS, but invalid on the server OS. You can still store the original filename is a database, if needed.
Solution 2:
Okay, I'm going to come out and say that you should probably just match the filename to your alphanumeric regex, and throw out a generic error if it doesn't match. However, if you are dead set on giving a specific warning for Chinese characters, use the following regex:
[\u4E00-\u9FFF]
A sharp eye may also recognize this as a subset of Japanese (since Japanese calls the Chinese character set 'Kanji').
if(preg_match("[\u4E00-\u9FFF]+", $filename))
{ echo"Chinese characters found in filename."; }
Solution 3:
this could work for non english characters
[^A-Za-z0-9_,.@\(\)\&\+\-\!\#\$\%\^\*\;\\\/\|\<\>'":\?\=\s]+
Solution 4:
get filename with this code:
functioncheckname(event) {
var fullPath = document.getElementById('file').value;
var filename;
if (fullPath) {
var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
filename = fullPath.substring(startIndex);
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
filename = filename.substring(1);
}
}
if (!/^[a-zA-Z0-9$@$!%*?&#^-_-. +]+$/.test(filename)) {
console.log("error: "+filename);
return;
} else {
console.log("ok");
}
};
<formaction=""method='post'enctype='multipart/form-data'><inputtype='file'id='file'name='file'onchange="checkname(event);"><inputtype='submit'value='upload'></form>
and use this code for validation:
/^[a-zA-Z0-9$@$!%*?&#^-_. +]+$/.test(filename);
if you want dont allow use characters use this:
/^[a-zA-Z0-9]+$/.test(filename);
Post a Comment for "Reject If File Name Contains Non English Character"