php - Regular expression filename -


i have site jquery/ajax want upload image. problem when have strange filename image. dots or other. have tried mode doesn't work fine, replace dot in file extension example if have

image.test.png 

become

imagetestpng 

but want this:

imagetest.png 

this code:

$name = $_files['upload']['name']; $size = $_files['upload']['size']; $name = preg_replace("/[^a-za-z0-9_\-]+/", "", $name); echo($name); 

how solve this? thanks

first, need decompose file name:

$info = pathinfo($name); 

then apply filter on both parts:

$name = preg_replace("/[^\w-]+/", "", $info['filename']); // check if have extension if (isset($info['extension'])) {     $name .= '.' . preg_replace('/[^\w]/', '', $info['extension']); } 

demo


Comments