Upload image directly through mySQL Command Line

49,591

Solution 1

Try using the LOAD_FILE() function.

UPDATE `certain_table`
SET image = LOAD_FILE('/full/path/to/new/image.jpg')
WHERE id = 1234;

See the manual for requirements about the path to the filename, privileges, etc.

Solution 2

LOAD_FILE works only with certain privileges and if the file is on the server. I've found out a way to make it work completely client side:

mysql -e "update mytable set image=FROM_BASE64('`base64 -i image.png`')" DBNAME

The idea is to encode the image to base64 on the fly and let then MySql decode it.

Solution 3

This is a variation on Teudimundo's answer that works with older MySQL versions, where Base64 functions are not available:

mysql -e "update mytable set col = x'$(xxd -p image.png | tr -d \\n)' where ..."

The trick is to use xxd -p to convert a binary file to a plain hexdump:

$ xxd -p /usr/share/font-manager/data/blank.png
89504e470d0a1a0a0000000d4948445200000040000000400806000000aa
6971de000000274944415478daedc1010d000000c220fba77e0e37600000
00000000000000000000000000c0bd0040400001ee1dbb2f000000004945
4e44ae426082

then using tr -d \\n to remove the newlines, and finally embedding the result into a MySQL-specific hexdump string literal: x'...'

Solution 4

I recommend you to never upload images directly in a database, it's quite inefficient. It's better to simply store the location and name of the image and store those images in a folder somewhere.

and if you want to "upload" via the commandline, you can just do an:

insert into table(image_loc) values('/images/random/cool.jpg') where id=1;

and depending on your environment you can use shell access to move images around. I'm not quite sure what you are trying to do with these images or how your system is setup. You'll probably need to clarify on that.

Share:
49,591
BurninatorDor
Author by

BurninatorDor

Updated on July 09, 2022

Comments

  • BurninatorDor
    BurninatorDor almost 2 years

    I have a certain table in mySQL which has a field called "image" with a datatype of "BLOB". I was wondering if it is possible to upload an image in that field directly from the Command Line Client rather than doing it through php...If it is possible, then where exactly should I place my image files?

  • BurninatorDor
    BurninatorDor over 12 years
    I have read a lot of online discussions regarding this issue...and I do support your views against it, but quite frankly I did not find enough documentation on how this process works....so I was kind of reluctant to approach it. If you have any links in mind, it would be great if you can post them here! :)
  • mugetsu
    mugetsu over 12 years
    @BurninatorDor what language do you use with mysql? It's as simple as executing a sql command to get the location string from the database and opening this file with your language. Personally I think using load_file is much more troubling.
  • BurninatorDor
    BurninatorDor over 12 years
    Yes, load_file() didnot work in my case so I am now planning on retrieving images from the system. I will be creating a web service in C# which will retrieve the location of the file from the mysql database...This is just the backend though....the front end is entirely in HTML5 + CSS3 + JavaScript. So I guess i will have to look for resources which will help me load an image from a given address in JavaScript maybe? Thanks for your help anyways...