• 0

[PHP] Storing images in MySQL database as Base64 string


Question

I am building a web site for a friend of mine and want to store images in a MySQL database as a Base64 string. I have seen this done before (embedding images in a web page as a Base64 string). My question is: how can I write a PHP script which will allow me to upload an image and convert it to a Base 64 string? Would it be better to store it as a binary file in the database? My goal is to not have a bunch of image files sitting around in a directory because my web host doesn't allow me to have write access by PHP scripts (I only have write access by FTP), but I need a way to upload images.

7 answers to this question

Recommended Posts

  • 0

Base64 disadvantages:

- size of data increases

- eats away (on a shared hosting - precious) processing time

Base64 advantages:

- uh... "telegraph-safe"?

Encoding:

// user_file is the name of the file upload control of your HTML form
$data = base64_encode(file_get_contents($_FILES['user_file']['tmp_name']));

It might be a good idea to validate the uploaded file, to check if it really is an image file.

If you have GD extension enabled, place the following check before actual reading and subsequent encoding:

if (!getimagesize($_FILES['user_file']['tmp_name']))

The function above will bungle if GD thinks the uploaded file doesn't paint a pretty picture, so to speak.

Decoding:

// you'll have to know MIME type of the image, though
// in this example image/png is used
echo '<img src="data:image/png;base64,"'.base64_decode($data).' alt="" />';

Note, however, that Data URI has limited support by IE8 (less than 32 KB and only in certain elements) and no support at all by versions before that!

Or you could connect to a directory using FTP capabilities of PHP. Quite like operating an appendix through one's mouth, though.

  • 0

If your web host doesn't allow PHP scripts to write files then I'd move to another web host if possible. Chances are that if they don't allow PHP scripts to write files then they aren't going to be very happy about you storing them in a database either (very large size for a database and it will use a lot of processing time, as cralias said).

Rather weird restriction in this day and age.

  • 0

i agree with fourjays.the best practice is always to save a pointer to where the image is stored,not the image itself.

by doing so you can decrease the database size, processing time and so on.

if you cannot write files using php or other type, move to another host asap.

  • 0

Storing images in the DB is a wonky solution which'll probably just kill your shared hosting.

 

Why don't you create a framework which can work with image sharing sites. For example, a function which uploads and then returns the direct URL of that image for you to then store in the DB. If there's no direct API's out there, you could use cURL and grep's to upload and retrieve the direct URL.

This topic is now closed to further replies.
  • Posts

    • Dead by Daylight, Two Point Museum, and more join Xbox Free Play Days for the weekend by Pulasthi Ariyasinghe There is a brand-new Free Play Days offer available to Xbox players, giving them a chance to try out a new selection of games over the weekend. Microsoft's latest promotion brings some high-profile titles, including the sports title PGA TOUR 2K25 and the racing sim Assetto Corsa. Almost all the games being offered this time require an Xbox Game Pass subscription, with only one title being available for all players. Starting with the fully free-to-play section, Dead by Daylight is populating it solo. This asymmetric survival horror title should be the most familiar to most gamers, considering its age. The multiplayer four-versus-one asymmetric survival horror game has you assuming the roles of survivors or the killer to see who can come out on top. This Dead by Daylight offer will be available to play until Monday, June 22, giving you an extra day of play compared to the remainder of the Free Play Days titles. Meanwhile, Xbox Game Pass Ultimate, Premium, and Essential members can now try out the well-received tycoon game Two Point Museum, the circuit racing sim Assetto Corsa, as well as 2K's golf sim experience that gives players a career to develop alongside real-world pros and courses. Here are the announced games and the platforms they are available to play on: PGA TOUR 2K25 (Xbox Series X|S) Two Point Museum (Xbox Series X|S) Assetto Corsa (Xbox Series X|S, Xbox One) Dead by Daylight (Xbox Series X|S, Xbox One) To easily find the titles on Xbox consoles, first head to the Store, and then in the sidebar, find the Home section. In there, open the Subscriptions tab. All the games from the Free Play Days collection will show up in this section for quick access. Apart from Dead by Daylight's offer, this week's Free Play Days promotions will end on Sunday, June 21, at 11:59 pm PT.
  • Recent Achievements

    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
    • One Month Later
      eurospharma62 earned a badge
      One Month Later
    • Week One Done
      With What earned a badge
      Week One Done
    • Week One Done
      Harris Gilbert earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      593
    2. 2
      +Edouard
      170
    3. 3
      PsYcHoKiLLa
      74
    4. 4
      Michael Scrip
      66
    5. 5
      ATLien_0
      64
  • Tell a friend

    Love Neowin? Tell a friend!