我将如何重命名base64图像

时间:2017-03-05 06:37:41

标签: php

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

这是我的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;
}
?>

1 个答案:

答案 0 :(得分:0)

尝试使用此代码上传重命名文件名的文件:

$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';
    }
}