PHPs base 64_decode is not converting base64 string to a real workable image file

后端 未结 4 1546
有刺的猬
有刺的猬 2021-01-28 04:26

Hello guys I successfully found a method that claims to make a file input file into a base 64 string in JavaScript so I successfully sent that base 64

string by JSON v

4条回答
  •  孤街浪徒
    2021-01-28 05:00

    Hey every one I found out for some reason the way the base 64 string is structured in the JSON seems to not be compatible in the PHP side. I did a test where I sent a base 64 encoded string in a JSON object to the PHP side and I compare it to a encoded base 64 string of the same file from

    https://www.browserling.com/tools/image-to-base64 in a if else statement for example if for it match and else where it says it don't match and to my surprise I found out it gave me the else statement meaning they don't match message so I found out that the way the base 64 string is

    encoded and structured in the JavaScript side is not being read right in the PHP side. That's why, any photo viewer apps could not read the photo. I assume its certain characters in the JSON encoded base 64 string but both of the compared strings from my if

    else statement test look exactly the same so I solved this problem by googling for a method that can escape special characters in the client side and I found this method call encodeURIComponent(); it escapes special characters but for URLs so I said well i'm

    ma try it to my surprise when I used that on the encoded base 64 string for the JSON object it successfully made that base 64 string understandable in the PHP server side and I was able to view the photo with no problems

    Here is my working example

    index.php

    
    
    
    
    
    
    
    
    

    x.php

    first_name;
    $last_name= $upload_info_json_object->last_name;
    
    
    //Photo upload section
    $photo= $upload_info_json_object->photo;
    
    //Decode into a file 
    $photo= base64_decode($photo); 
    
    file_put_contents('geeksforgeeks-22.jpg',$photo);
    
    //
    
    ?>
    
    

    so my problem now is if I try to use the encodeURIComponent(); method on a video encoded in a base 64 string it gave me this error

    Uncaught RangeError: Invalid string length

    So I need to find another method that escape the characters in the encoded base 64 string in the JSON object on the client side to have it to be understandable in the PHP side in other words something like what encodeURIComponent(); does but not for URLs but for a base 64 encoded string on the client regardless how big the encoded base 64 string is on the client side.

提交回复
热议问题