Hi,
I have spent hours (literally) tring to figure out the regular expression to clean the name of an uploaded file.
I find that my clients often try to upload non-images, so I can check if its a '.jpg', '.gif' etc, but what about if they name their files 'cupboard.html.jpg'?? (its happened!)
This is what I have so far (after checking the file):
$photo = 'cupboard and other illegal chars' here!@#$%^&*(.html.jpg';
if (preg_match("/jpg|jpeg/i",$photo)){$image_type=".jpg";}
if (preg_match("/png/i",$photo)){$image_type=".png";}
if (preg_match("/gif/i",$photo)){$image_type=".gif";}
// Don't process non-images
if (eregi('.jpg|.jpeg|.gif|.png',$photo)) {
// Take out illegal char in name
list($name)=explode($image_type, $photo);
$name = $this->cleanString($name);
$photo_name = $name.$image_type;
// Rename file
rename($dir.$photo, $dir.$photo_name);
// Thumb photo using GD
createdetail($photo_name, $dir.$photo_name, $thumb_w, $thumb_h);
}
// Clean string to only Alpha Numeric char
function cleanString($content) {
return ereg_replace("[^[:alnum:]+]","", ereg_replace(" ","_", strtolower($content)));
}
This takes out all spaces and most problematic characters (I think), but what I want is to show spaced in the filename as underscores (_) as well...
Any ideas?
Thanks.