• 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

    • Heaven forbid they lose pennies from their Trillions! Like always, the consumer pays the most. Why is Tim Cooks even talking.....shouldn't he be packing up his office??
    • If you have the budget...! Some solo or indies just want to either learn or start their game and aren't in a capacity to pay salaries or to contractors... Get real.
    • Source and more 35 years old?! And if my maths is mathing, that means she was around 10 when The Ring came out?! Damn...scariest 10 year old I think I've ever seen. 
    • Adobe Acrobat Reader DC 2026.001.21677 by Razvan Serea Adobe Acrobat Reader DC software is the free, trusted standard for viewing, printing, signing, and annotating PDFs. Its the only PDF viewer that can open and interact with all types of PDF content – including forms and multimedia. It’s connected to Adobe Document Cloud – so you can work with PDFs on computers and mobile devices. Adobe Document Cloud is a revolutionary, modern and efficient way to get work done with documents in the office, at home or on-the-go. At the heart of Document Cloud is the all-new Adobe Acrobat DC, which will take e-signatures mainstream by delivering free e-signing with every individual subscription. Document Cloud includes a set of integrated services that use a consistent online profile and personal document hub. With Adobe Document Cloud, people will be able to create, review, approve, sign and track documents whether on a desktop or mobile device. Businesses will be able to take advantage of Document Cloud for enterprise which provides enterprise-class document services that integrate into systems of record such as CRM, HCM, CLM, and CMS, adding speed, efficiency and transparency to getting business done with documents. Adobe Acrobat Reader DC new feature highlights: Work with PDFs from anywhere with the new, free Acrobat DC mobile app for Android or iOS. Select functionality is also available on Windows Phone. Use the new Fill & Sign tool in your desktop software to complete PDF forms fast with smart autofill. Download the free Adobe Fill & Sign mobile app to add the same option to your iPad or Android tablet device. Save money on ink and toner when printing from your Windows PC. Store and access files in Adobe Document Cloud with 5GB of free storage. Get instant access to recent files across desktop, web, and mobile devices with Mobile Link. Sync your Fill & Sign autofill collection across desktop, web, and iPad devices. Adobe PDF Pack premium features includes: Convert documents and images to PDF files. Use your mobile device camera to take a picture of a paper document or form and convert it to PDF. Turn PDFs into editable Microsoft Word, Excel, PowerPoint, or RTF files. Combine multiple files into a single PDF (web only). Get signatures from others with a complete e-signature service. Send, track, and confirm delivery of documents electronically instead of using fax or overnight services (tracking not available on mobile). Store and access files online with 20GB of storage. Download: Adobe Acrobat Reader DC 64-bit | 719.0 MB (Freeware) Link: Adobe Acrobat Reader DC Home Page | Release Notes | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Everybody will complain, but probably will sell like hotcakes......
  • Recent Achievements

    • 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
    • One Month Later
      Vincian earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      538
    2. 2
      +Edouard
      168
    3. 3
      PsYcHoKiLLa
      75
    4. 4
      neufuse
      64
    5. 5
      ATLien_0
      63
  • Tell a friend

    Love Neowin? Tell a friend!