1. 程式人生 > >php 圖片上傳

php 圖片上傳

private function base64_upload($base64) {
		ini_set('max_execution_time', '0');
    	$base64_image = str_replace(' ', '+', $base64);
    	if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
    		//匹配成功
    		$image_name = uniqid().'.'.$result[2];
    		$image_file = "./uploads/app/{$image_name}";
    		//判斷後綴
    		if($result[2] != 'jpeg' || $result[2] != 'jpg'){
    			//return false;
    		}
    		$base64_decode = base64_decode(str_replace($result[1], '', $base64_image));
    
    		//影象檢查
    		$im = imagecreatefromstring($base64_decode);
    		if($im === false){
    			imagedestroy ($im);
    			//return false;
    		}
    		imagedestroy ($im);
    		//伺服器檔案儲存路徑
    		if(file_put_contents($image_file, $base64_decode, LOCK_EX)){
    			@unlink(RUNTIME_FILE);
    			return "/uploads/app/{$image_name}";
    		} else {
    		return false;
    		}
    		}else{
    		return false;
    		}
	}