简体   繁体   中英

how to base64 encode a doc file

I am working on a php project that I need to send base64 encoded doc document to the server using CURL. Here is the code: $c = file_get_contents("test.doc"); $encoded = base64_encode($c);

I found the resulting $encoded is invalid.

In this website: https://www.base64encode.org/ I uploaded the file and base64 encoded online, the resultant encoded string is correct.

I then tried to cut and paste the text from the doc file and encoded them online at the above website, the resultant string is different than the valid string I got. Therefore, I guess I could not just extract the text from the doc file and base64 encode them.

try this-

$c =  file_get_contents("test.doc");
file_put_contents('temp.txt',$c);
$cc = file_get_contents('temp.txt');
if(strlen($cc)=='0'){
$cc = fopen("temp.txt","r");}
$encoded = base64_encode($cc);
unlink('temp.txt');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM