Wednesday, 15 June 2016

PHP Save Image in Base64 Encode Format

Save image in database in base64_encode format.

1. Create HTML Form

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="to_upload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

2. Now in upload.php
if(!empty($_FILES)){
    $extension = pathinfo($_FILES["to_upload"]["tmp_name"], PATHINFO_EXTENSION);
    $image_content = file_get_contents($_FILES["to_upload"]["tmp_name"]);
    $image_content = base64_encode($image_content);
    $binaryData = 'data:image/' . $extension . ';base64,' . $image_content;
}
Store $binaryData value in database.

3. Html form your you need to display.
<img src="<?php echo $binaryData;?> " title="Display Image from database file" />';

0 comments:

Post a Comment