Hướng dẫn how to convert uploaded file to base64 in php? - làm thế nào để chuyển đổi tập tin đã tải lên base64 trong php?

Tôi biết cách tải lên tệp hình ảnh và lưu đến vị trí khác bằng cách sử dụng mã sau. Tuy nhiên, tôi cần thực hiện theo cách mà người dùng tải lên hình ảnh và tự động chuyển đổi sang Base64 mà không lưu hình ảnh đó ở vị trí của tôi. Tôi nên làm thế nào?

";

    $type = pathinfo($file_tmp, PATHINFO_EXTENSION);
    $data = file_get_contents($file_ext);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
    echo "Base64 is ".$base64;



    if(in_array($file_ext,$allowed_ext) === false)
    {
        $errors[]='Extension not allowed';
    }

    if($file_size > 2097152)
    {
        $errors[]= 'File size must be under 2mb';

    }
    if(empty($errors))
    {
       if( move_uploaded_file($file_tmp, 'images/'.$file_name));
       {
        echo 'File uploaded';
       }
    }
    else
    {
        foreach($errors as $error)
        {
            echo $error , '
'; } } // print_r($errors); } ?>

Hỏi ngày 12 tháng 10 năm 2013 lúc 14:50Oct 12, 2013 at 14:50

Hướng dẫn how to convert uploaded file to base64 in php? - làm thế nào để chuyển đổi tập tin đã tải lên base64 trong php?

Khant thu Linnkhant Thu LinnKhant Thu Linn

5.6557 Huy hiệu vàng49 Huy hiệu bạc114 Huy hiệu đồng7 gold badges49 silver badges114 bronze badges

14

Có một sai lầm trong mã của bạn:

$data = file_get_contents( $file_ext );

Điều này nên là:

$data = file_get_contents( $file_tmp );

Điều này sẽ giải quyết vấn đề của bạn.

Đã trả lời ngày 16 tháng 2 năm 2015 lúc 11:16Feb 16, 2015 at 11:16

Hướng dẫn how to convert uploaded file to base64 in php? - làm thế nào để chuyển đổi tập tin đã tải lên base64 trong php?

halfpastfour.amhalfpastfour.amhalfpastfour.am

5.5443 Huy hiệu vàng41 Huy hiệu bạc59 Huy hiệu Đồng3 gold badges41 silver badges59 bronze badges

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    • Hàm base64_encode () là một hàm sẵn có trong PHP được sử dụng để chuyển đổi bất kỳ dữ liệu nào thành mã hóa base64. Để chuyển đổi một hình ảnh thành mã hóa Base64 trước tiên cần phải lấy nội dung của tệp. Điều này có thể được thực hiện với chức năng trợ giúp của file_get_contents () của PHP. Sau đó chuyển dữ liệu thô này đến hàm base64_encode () để mã hóa. The base64_encode() function is an inbuilt function in PHP which is used to Encodes data with MIME base64. MIME (Multipurpose Internet Mail Extensions) base64 is used to encode the string in base64. The base64_encoded data takes 33% more space then original data.
    • Chức năng bắt buộc: The file_get_contents() function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are supported by the server and thus enhances the performances making it a preferred way of reading contents of a file.

    Chức năng base64_encode () hàm base64_encode () là một hàm sẵn có trong PHP được sử dụng để mã hóa dữ liệu có cơ sở MIME64. MIME (Phần mở rộng thư Internet đa năng) Base64 được sử dụng để mã hóa chuỗi trong Base64. Dữ liệu base64_encoded mất thêm 33% không gian sau đó là dữ liệu gốc.

    Hướng dẫn how to convert uploaded file to base64 in php? - làm thế nào để chuyển đổi tập tin đã tải lên base64 trong php?

    Program:

    Output:

    iVBORw0KGgoAAAANSUhEUgAAApsAAAC4CAYAAACsNSfVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJ
    cEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJdhfdsglgklAEFkb2JlIEltYWdlUmVhZHlxyWqwrwtwefd
    ...
    TeUsalQKBQKhUKhsBvK2FQoFAqFQqFQ2A1lbCoUCoVCoVAo7IYyNhUKhUKhUCgUdkMZmwqFQKBQKO0H0fxpZ1bfc

    Reference:

    • http://php.net/manual/en/function.base64-encode.php
    • http://php.net/manual/en/function.file-get-contents.php

    file_get_contents () hàm hàm file_get_contents () trong PHP là một hàm sẵn được sử dụng để đọc một tệp vào một chuỗi. Chức năng sử dụng các kỹ thuật ánh xạ bộ nhớ được máy chủ hỗ trợ và do đó nâng cao hiệu suất làm cho nó trở thành một cách đọc nội dung ưa thích của một tệp.

    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 vào thư mục.Base64 được mã hóa.Vì vậy, trong trường hợp đó, bạn sẽ cần di chuyển hình ảnh được mã hóa base64 sang máy chủ dưới dạng tệp hình ảnh. Và phải lưu nó trong thư mục
    While we have working with API for mobile or web application , You will notice that they will send the format of images in Base64 encoded. So in that case, you will need to move the Base64 encoded image to server as a image file.and have to save it in the folder

    Ví dụ php cốt lõi:

     

    Tải lên cơ sở64 để tệp trong codeigniter & nbsp;

    Bạn đang tìm kiếm một 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 có thể giúp bạn với điều đó.

    Chúng tôi đang tạo một số chức năng để sử dụng kiểm tra chuỗi cơ sở64 có hợp lệ hay không và kích thước và loại tệp

    Tạo ứng dụng tệp/thư viện/base64fileuploads.phpapplication/libraries/Base64fileUploads.php

    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 Codeigniter & nbsp; trong Trình điều khiển & NBSP;

    
    $base64file   = new Base64fileUploads();
    $return = $base64file->du_uploads($this->data['document'],$document_front_site);