I have read numerous articles on stackoverflow and google regarding file upload and show directly in/from MYSQL BLOB column. I donot need to upload file anywhere because only one file is involved in my complete project and that is the logo file to be updated by the user. Although I wanted to do the things with Codeigniter Upload library, but i couldn't complete the code so I was trying simple PHP solution, but it hasnot worked either.
Below is my code.
Code for Upload Form:
<?php echo form_open_multipart('UpdateCompanyInfo'); ?>
<div class="form-group">
<label>Logo</label>
<input type="file" class="form-control" name="logo">
[200 px (width)x200px (height)]
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Update Logo</button>
</div>
<?php echo form_close(); ?>
Code for Upload:
$check = getimagesize($_FILES["logo"]["tmp_name"]);
if($check !== false)
{
if($check[0]=="200" && $check[1]=="200" )
{
$image = $_FILES['logo']['tmp_name'];
$imageFileType = strtolower(pathinfo($_FILES['logo']['name'],PATHINFO_EXTENSION));
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
return '<div class="alert alert-danger">'.$imageFileType.' Logo file of only jpg/png/jpeg type is acceptable.</div>';
}
$imgContent = addslashes(file_get_contents($image));
if($this->db->update("company", array('logo'=>$imgContent)))
{
return '<div class="alert alert-success">Logo has been updated successfully.</div>';
}
else
{
return '<div class="alert alert-danger">Error 101. Failed to update the logo.</div>';
}
}
else
return '<div class="alert alert-danger">Logo file of only 200px X 200px is acceptable.</div>';
}
Code to show the Image in View
echo '<img src="data:image/png;base64,'.base64_encode($companyinfo[0]->logo).'"/>';
I uploaded the png file, so I have used data:image/png
On inspecting the image element in view, I get the following output:
<img src="data:image/png;base64,iVBORw0KGgpcMFwwXDAN ....... v6/IDhCwtLWDE56vdo6CwCC07dMeva1dhgIGnEBikg7xODG0Pq+m61z8bC4Vqpzxpq6d8BhQK7Q1+rte/oyhK61mIAoGRznxWpiYmb8jaleunKEotrSaXy1Xb1lnxXhXvSAGgKtXBetZJOQEAIQkBAQAhCQEAIQkBACEJA8P7xP2GoiDrA7B2BXDBcMFwwXDBJRU5ErkJggg==">
All articles over internet are showing this solution, but why it is not working at my end?