基於ThinkPHP解決了前端以base64形式傳文件實現文件上傳的函數封裝


/**
* 封裝base64位圖片上傳
*/
function base64_upload($base64) {
$base64_image = str_replace(' ', '+', $base64);
//post的數據里面,加號會被替換為空格,需要重新替換回來,如果不是post的數據,則注釋掉這一行
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
//匹配成功
if($result[2] == 'jpeg'){
$image_name = uniqid().'.jpg';
//純粹是看jpeg不爽才替換的
}else{
$image_name = uniqid().'.'.$result[2];
}
$image_file = "./Public/Personal/{$image_name}";
//服務器文件存儲路徑
if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){
return '/Public/Personal/'.$image_name;
}else{
return false;
}
}else{
return false;
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM