Using base_64encode for images

The primary problem of a developer is

How to send image from the front end to the back end server which is running on PHP.

So the process goes like this

1> Create a form in HTML to allow user to add an image

<form action ="imageLoader.php" method="post" encType ="multipart/form-data">

<input type ="file" name="myImage" />

<input type="submit" name="submit" />


The requirement is to display the file in another script.

In PHP script imageLoader.php

$_FILES will provide the file name which can be copied to another location and then read using file_get_contents()

$content = file_get_contents("myfile.png");

$base64String = base64_encode($content);

Based on the type type add

Then the base64String can be re-sent to the server using a script like this

header('Content-Type: application/json');

echo json_encode($base64String);


Don’t forget to include die(); otherwise anything after the script will be sent as well

Leave a Reply

Your email address will not be published.