• 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

    • The development time of this Operating System will have competition with the development time of Star Citizen 😂
    • I saw the 300 in the image and thought it was the number of cores! 🤣
    • Threads is getting a feature Reddit has had for nearly a decade by David Uzondu Back in January, reverse engineer Alessandro Paluzzi (@alex193a) first uncovered that Threads was working on spoiler tags. Now, Meta CEO Mark Zuckerberg (@zuck) has made it official: the feature is in testing. It is a straightforward tool for hiding content, especially text, behind a block that you have to click to reveal, perfect for discussing the ending of your favorite show without ruining the experience for others. It is also, notably, a feature that Reddit has had since January 2017. https://www.threads.com/@zuck/post/DK-BydcJHkF For a platform aiming to be a town square for public discourse, not having a native way to handle spoilers is a small but noticeable handicap for communities focused on pop culture. In a press release, Meta told TechCrunch that popular shows like The Last of Us trended heavily on the app, which the company frames as a sign of a "growing community of entertainment and pop culture enthusiasts." The new feature also supports images so that you can hide shocking visuals or text-filled screenshots. From the images Meta shared, it seems to work just as Paluzzi uncovered months ago. Users with access to the test can highlight text in the composer, and a "mark spoiler" option will appear in a small menu. This method is purely button-based, unlike on Reddit, where users have the option of either using a similar editor button or manually typing out the markdown command >!like this!< to hide their text. This update does not exist in a vacuum. After several months of feeling incomplete, the platform has been getting features that users have actually been asking for. We have seen this with the recent DMs feature, also in testing, which finally gives people a way to have private chats without jumping over to other platforms like Instagram.
    • The cars are on the road in multiple cities and about to launch in the UK early next year. Robotaxi is yet to be seen on the streets of any city. Nobody here has mentioned violence. That's all in your head. Have you thought about talking to somebody about it?
    • I don't get why this company needs to change sockets every 2 generations, I really don't. If AMD can deliver a superior product both in terms of performance and energy consumption on the same socket for multiple generations, why can't Intel? I don't like to wear the tinfoil hat, but come on, this stinks of backroom deals with motherboard manufacturers? New socket = new motherboards = more sales.
  • 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
      658
    2. 2
      ATLien_0
      272
    3. 3
      +FloatingFatMan
      176
    4. 4
      Michael Scrip
      157
    5. 5
      Steven P.
      136
  • Tell a friend

    Love Neowin? Tell a friend!