如何在完成UPLOAD后删除空格和特殊字符?
$ds = DIRECTORY_SEPARATOR; //1
$storeFolder = 'arquivos'; //2
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; //4
$targetFile = $targetPath. $_FILES['file']['name']; //5
move_uploaded_file($tempFile,$targetFile); //6
}
答案 0 :(得分:2)
只需改变一下:
$targetFile = $targetPath. $_FILES['file']['name']; //5
move_uploaded_file($tempFile,$targetFile); //6
添加一些名称处理。这样你就可以存储文件并在同一个地方更改名称。
$originalName = $_FILES['file']['name'];
$safeName = cleanNameFunction($originalName);
$targetFile = $targetPath . $safeName; //5
move_uploaded_file($tempFile, $targetFile); //6
例如,cleanNameFunction
可以删除空格。
除了a-zA-Z0-9
之外,我通常会删除所有内容。
function cleanNameFunction($name){
$name = preg_replace("/[^a-zA-Z0-9]+/", "", $name);
return $name;
}