Bạn đang tìm cách lưu phía máy chủ hình ảnh PNG, từ chuỗi dữ liệu base64 hoặc không thể tải lên hình ảnh được mã hóa base64 bằng Codeigniter?
Chúng tôi đang tạo một số chức năng để sử dụng kiểm tra chuỗi base64 có hợp lệ hay không, kích thước và loại tệp
tạo ứng dụng tệp/thư viện/Base64fileUploads. php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Base64fileUploads { function is_base64($s){ // Check if there are valid base64 characters // if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s)) return false; // Decode the string in strict mode and check the results $decoded = base64_decode($s, true); if(false === $decoded) return false; // Encode the string again if(base64_encode($decoded) != $s) return false; return true; } public function du_uploads($path,$base64string){ if( $this->is_base64($base64string) == true ){ $base64string = "data:image/jpeg;base64,".$base64string; $this->check_size($base64string); $this->check_dir($path); $this->check_file_type($base64string); /*=================uploads=================*/ list($type, $base64string) = explode(';', $base64string); list(,$extension) = explode('/',$type); list(,$base64string) = explode(',', $base64string); $fileName = uniqid().date('Y_m_d').'.'.$extension; $base64string = base64_decode($base64string); file_put_contents($path.$fileName, $base64string); return array('status' =>true,'message' =>'successfully upload !','file_name'=>$fileName,'with_path'=>$path.$fileName); }else{ print_r(json_encode(array('status' =>false,'message' => 'This Base64 String not allowed !')));exit; } } public function check_size($base64string){ $file_size = 8000000; $size = @getimagesize($base64string); if($size['bits'] >= $file_size){ print_r(json_encode(array('status' =>false,'message' => 'file size not allowed !')));exit; } return true; } public function check_dir($path){ if (!file_exists($path)) { mkdir($path, 0777, true); return true; } return true; } public function check_file_type($base64string){ $mime_type = @mime_content_type($base64string); $allowed_file_types = ['image/png', 'image/jpeg', 'application/pdf']; if (! in_array($mime_type, $allowed_file_types)) { // File type is NOT allowed // print_r(json_encode(array('status' =>false,'message' => 'File type is NOT allowed !')));exit; } return true; } }Ví dụ Codeigniter
Gọi lớp này là Codeigniter trong bộ điều khiển
$base64file = new Base64fileUploads(); $return = $base64file->du_uploads($this->data['document'],$document_front_site);
0 bình luận
- Chia sẻ cái này
Cách tải base64 lên tệp trong PHP
1 năm trước 14 nghìn lượt xem
Đăng nhập bằng Facebook trong CodeIgniter 3. 1. 10- 5. 6 ở trên
3 năm trước 14 nghìn lượt xem
Để lại một câu trả lời
Bạn phải đăng nhập để bình luận Cách tải base64 lên tệp trong PHP. Nhấp vào Đăng nhập
đăng bình luận
Bài viết liên quan
php,base64,tệp,tải lên
Cách tải base64 lên tệp trong PHPCách tải base64 lên tệp trong PHP Trong hướng dẫn này, chúng tôi sẽ cho bạn biết cách xử lý hình ảnh được mã hóa bằng Base64 và viết hình ảnh
11 tháng trước Ngăn xếp
php, codeIgniter, facebook
Đăng nhập bằng Facebook trong CodeIgniter 3. 1. 10- 5. 6 ở trênTích hợp đăng nhập Facebook là tính năng được ứng dụng web sử dụng nhiều nhất hiện nay. Tính năng đăng nhập bằng Facebook giúp người dùng đăng nhập vào web appli
1 năm trước K. K ADIL KHAN AZAD
laravel, Ubuntu, linux
Cách cài đặt phiên bản laravel mới nhất trên Ubuntu 18. 04. 20. 04Chào mừng. các bạn hôm nay Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách cài đặt Laravel trên Ubuntu 20. * hoặc 20. *+ hệ thống. Các hướng dẫn tương tự áp dụng cho
1 năm trước Ngăn xếp
Linux, Flutter, Ubuntu
Cách cài đặt Flutter trên Ubuntu 20. 04 Hoặc 18. 04 Hoặc 21. 04LinuxFlutter là một SDK mã nguồn mở được phát triển để lập trình các ứng dụng hoạt động trên nhiều hệ điều hành sử dụng một cơ sở mã duy nhất như Android, i