我想知道是否可以更改要上传的文件的名称。我的意思是我想做的是,用户上传一个文件,该文件可能包含一些特殊字符,例如某些欧洲语言中的特殊字符。
我打算做的是,在使用 move_uploaded_file 命令之前,是否可以将特殊字符更改/preg_replace 为普通字符,以便文件以只有普通字符的新名称上传和存储。
最佳答案
// Get the original file name from $_FILES
$file_name= $_FILES['file']['name'];
// Remove any characters you don't want
// The below code will remove anything that is not a-z, 0-9 or a dot.
$file_name = preg_replace("/[^a-zA-Z0-9.]/", "", $file_name);
// Get the location of the folder to upload into
$location = 'path/to/dir/';
// Use move_uploaded_file()
move_uploaded_file($_FILES["file"]["tmp_name"], $location.$file_name);
关于php - 在使用 PHP 上传文件之前替换特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838994/