2017-03-05 69 views
-1

在將圖像上傳到我的數據庫服務器之前,我將如何根據將其上傳到base64字符串的用戶的用戶標識重命名圖像。我將如何重命名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; 
} 
?> 
+0

您需要通過用戶ID作爲一個參數或將其存儲在會話時用戶登錄。 –

回答

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'; 
    } 
} 
+0

好吧我試試吧,謝謝 – Presmelito