即时通讯使用php将图像上传到我的网站,我想在上传后立即将它们重命名,然后将它们放入服务器目录中,这样我就可以消除图像被覆盖的可能性。文件,下面是我试过的代码:
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
$uploaddir = 'tmp-uploads/';
$newFileName = generateRandomString(20);
rename($_FILES['userfile']['name'], $newFileName);
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// do something else
}
答案 0 :(得分:0)
$_FILES['userfile']['name']
只是变量中的名称,它不是文件,如果要更改其值,只需使用赋值。
$newFileName = generateRandomString(20);
$_FILES['userfile']['name'] = $newFileName;
或仅使用$newFileName
代替$_FILES['userfile']['name']