0

I am drawing a canvas and I convert it to dataUrl before I save it into database. The database column for the image is longblob type.

$img = $_POST['picture_dataUrl'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$fileData = base64_decode($img);
//saving
$fileName = 'photo5.png';

file_put_contents($fileName, $fileData);
file_put_contents('imgname.txt', $fileData);

//Example of Updating the database
CheckIn::updateAll(['photo1' => $fileData],['id'=>11]);

I have already successfully update / save it into the database. The question is when I wanted to download the image DIRECTLY (click on the image link in database) from database, it works but it being download with the extension .bin, example like photo5.bin, I need to change it myself to jpg, png etc.

Is there anyway to change the extension of the image when I download directly from database? Thanks

Shadow
  • 33,525
  • 10
  • 51
  • 64
ron
  • 175
  • 3
  • 14
  • Why is your title about saving blob using PHPMyAdmin when you state that you've successfully saved the image in the DB? It's very confusing. Where is the code for fetching and downloading the image? All I can see is you getting some post data which you store in a file _and_ in the database? (Why store the images in a DB to start with?) – M. Eriksson Jul 10 '17 at 08:15
  • Please DO NOT SAVE IMAGES AS BLOB IN DATABASE. There is chances you going to slow your application. Going to render corrupt images. Going to use more resources in database. Going to slow your query. ..and so on... – Nono Jul 10 '17 at 08:16
  • I try to change the title. Dun have code for downloading image, what I meant is I go to phpmyadmin database I click the column from the database which store the image, example inside the column it show ` [BLOB - 177.2 KiB]`. I just click on it and it will auto download – ron Jul 10 '17 at 08:19
  • PHPMyAdmin only knows that there's binary data. It has no way of knowing what the filename or extension should be. Why do you need to download the images through PHPMyAdmin? Why not just store the images on the server as files and store the filename in the DB? – M. Eriksson Jul 10 '17 at 08:28
  • Ok I will try look into it. Thanks for the suggestion~ – ron Jul 10 '17 at 08:34
  • I cannot provide an answer as your question has been marked as a duplicate. However, although I don't recommend saving file in the database you can still do it as long as you Optimize it for that. Please take a look at the following links https://dev.mysql.com/doc/refman/5.5/en/optimize-blob.html and https://stackoverflow.com/questions/9511476/speed-of-mysql-query-on-tables-containing-blob-depends-on-filesystem-cache – thedethfox Jul 10 '17 at 09:28
  • If you go with saving in the database then don't forget to set the correct header before you send the file to client. Please look at https://stackoverflow.com/questions/8485886/force-file-download-with-php-using-header and http://php.net/manual/en/function.mime-content-type.php – thedethfox Jul 10 '17 at 09:30
  • Finally if you want to save the files on the server then please consider using a technique that allows you save a large set of files on the file system like the following demos https://stackoverflow.com/questions/23787785/how-to-use-hash-function-for-storing-4-million-images-in-file-system – thedethfox Jul 10 '17 at 09:31

0 Answers0