0

I have a base64 encoded string converted with the php function below.

public function convert_image_to_base64($file_path){
    $mime = get_mime_by_extension($file_path);
    $data = file_get_contents($file_path);
        
    return 'data:'. $mime . ';base64,' . base64_encode($data);      
}

I want to download the file in a browser with the below html

<a href="<?= $base64_file ?>" target="blank">Download</a>

When I click, the link downloads a zip file.

This is the start of the resulting base64 string

data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UEsDBBQABgAIAAAAI

I want to download the exact .docx or .doc file uploaded

I searched on stackoverflow and other forums but none of them address this exact problem. I also don't want to decode the file before submitting the html.

Nafiu Lawal
  • 447
  • 1
  • 7
  • 16

1 Answers1

0

So the problem was the file extension of the downloaded file was not added in the base64 file downloaded. I solved the problem by forcing the name of the downloaded file using the html below from this link. Reference here

<a href="<?= $base64_file ?>" download="file.docx" target="blank">Download</a>
Nafiu Lawal
  • 447
  • 1
  • 7
  • 16