Php 如何重命名base64映像

Php 如何重命名base64映像,php,Php,在将图像上传到我的数据库服务器之前,我将如何根据上传图像的用户id重命名图像,并将其编码为base64字符串 这是我的PHP代码 <?PHP if(isset($_POST['image'])){ $now = DateTime::createFromFormat('U.u', microtime(true)); $id = $now->format('YmdHisu'); $upload_folder = "upload"; $path = "$upload_folder/$id.

在将图像上传到我的数据库服务器之前,我将如何根据上传图像的用户id重命名图像,并将其编码为base64字符串

这是我的PHP代码

<?PHP
if(isset($_POST['image'])){
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = $now->format('YmdHisu');
$upload_folder = "upload";
$path = "$upload_folder/$id.jpeg";
$image = $_POST['image'];
if(file_put_contents($path, base64_decode($image)) != false){
echo "upload_success";
exit;
}
else{
echo "upload_failed";
exit;
}
}
else{
echo"image_not_in";
exit;
}
?>

尝试使用以下代码上载重命名文件名的文件:

$name = your_file_type_name_here;
if (isset($_FILES[$name]) && isset($_FILES[$name]['tmp_name']) && !empty($_FILES[$name]['tmp_name'])) {
    $uploadDir = Upload_destination_directory_here;
    $ext = substr($_FILES[$name]['name'], strrpos($_FILES[$name]['name'], '.') + 1);
    $user_id = your_user_id_here;
    $fileDecodename = base64_encode($user_id);
    $file_name = $fileDecodename.'.'.$ext;
    $mainFile = $uploadDir.$file_name;
    if(!move_uploaded_file($_FILES[$name]['tmp_name'],$mainFile)) {
        print 'upload faild';
    }else{
        print 'upload successfull';
    }
}

您需要将用户id作为参数传递,或者在用户登录时将其存储在会话中。