Coding Cheatsheets - Learn web development code and tutorials for Software developers which will helps you in project. Get help on JavaScript, PHP, XML, and more.

Post Page Advertisement [Top]

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" />';

No comments:

Post a Comment

Bottom Ad [Post Page]