haugstrup/filethingie — pull request #15 by acomjean (2013-01-27 05:35:27+00:00)
Title: Regular expression seems to cause problems in new file validator function

The new file validate function seems to indentify any file with a " " in it as a non valid file.
$pattern should also start and end with /, without it gives an warning.

Warning: preg_match() [function.preg-match]: Unknown modifier '\' in /www/www/test/filethingie/ft2.php on line 113

I changed ft_validate_filename so it rejects only files with .. Not as good, but at least it doesn't false flag files as "File type not allowed"

function ft_validate_filename($file)
{
// Make sure the file doesn't start with a period, contain ".." or end in a period.
$pattern = '/^[^\.]((\.)?(([\w~!]){1,}))+$/';
$result = preg_match($pattern, $file);

return $result;
}
// my test

$filenames= array("file", "file2", "file.jpg","file space.jpg"."../file","../", "file.", ".file");

foreach ($filenames as $oneFileName){
$result =	ft_validate_filename($oneFileName);

echo "$oneFileName -> ";
if ($result){
echo "Valid
";
}else{
echo "Not Valid
";
}
}
//Results
file -> Valid
file2 -> Valid
file.jpg -> Valid
file space.jpg../file -> Not Valid
../ -> Not Valid
file. -> Not Valid
.file -> Not Valid