• 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

    • No, it's 130 Euro if you want the entire game, not just a third of it.
    • Glad they aren't hiking the price $10. If anything they should offer a $10 discount to anyone that provides a receipt for that awful movie.
    • At this point, with this - they have proven it to be a gimmick to “look like Windows”, rather than “here’s a nice useable OS”
    • AMD releases new Windows 10 and 11 chipset driver for Ryzen processors by Taras Buria Following a major firmware update for a TPM-Pluton security flaw, AMD has a new chipset driver for all of its supported Ryzen platforms and processors. Version 7.06.02.123 is now available for download, and it adds unnamed bug fixes and an important addition to the AMD PPM Provisioning File driver, which improves the security of the component. The security update in question is CETCOMPAT support for the AMD PPM Provisioning File driver. CET or Control-flow Enforcement Technology (CET) was introduced for Windows 10 versions 20H1 and 20H2 in early 2021. In a nutshell, it is an Intel-developed protection system that prevents malware from changing memory addresses (Shadow Stack) or redirecting programs to malicious code by ensuring they use only approved, safe locations (Indirect Branch Tracking). AMD brought CETCOMPACT support to parts of its Ryzen chipset driver suite (the driver you download and install is a set of different drivers serving different purposes that AMD lets you select on the first screen) in October 2024. Now, with driver 7.06.02.123, CET compatibility has been added to the AMD PPM Provisioning File driver, which is responsible for core parking and power management. Here is the list of updated drivers in today's release: Driver Version (Windows 10) Version (Windows 11) Changelog AMD PPM Provisioning File Driver 8.0.0.53 8.0.0.53 CETCOMPAT support is added AMD PMF-6000 Series Driver 24.0.6.0 24.0.6.0 Bug fixes AMD S0i3 Filter Driver 4.5.0.1020 4.5.0.1020 AMD 3D V-Cache Performance Optimizer Driver 1.0.0.11 1.0.0.11 AMD PMF-7040 Series Driver 24.2.6.0 24.2.6.0 AMD PMF-8000Series Driver 25.5.25.0 25.5.25.0 AMD PMF Ryzen AI 300 Series Driver 1 Not Applicable 25.6.28.0 AMD's latest Ryzen chipset driver supports 64-bit Windows 10 and 11 systems with the following motherboards: A-Series B-Series X-Series Threadripper AMD A320 AMD A520 AMD B350 AMD B450 AMD B550 AMD B650 / B650E AMD X370 AMD X470 AMD X570 AMD X670 / X670E AMD X870 / X870E AMD TRX40 AMD TRX50 AMD WRX80 AMD WRX90 You can download AMD Ryzen Chipset Driver 7.0.6.02.123 from the official support website. Full release notes and additional information is available on the same page.
    • I never got to experience BOB Hello even now because gaming laptops I bought always omitted the IR camera. And the standalone cameras for BOB Hello are super expensive
  • Recent Achievements

    • Explorer
      treker_ed went up a rank
      Explorer
    • Apprentice
      CHUNWEI went up a rank
      Apprentice
    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      641
    2. 2
      ATLien_0
      272
    3. 3
      +FloatingFatMan
      174
    4. 4
      Michael Scrip
      157
    5. 5
      Steven P.
      134
  • Tell a friend

    Love Neowin? Tell a friend!