Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload image directly through mySQL Command Line

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?

like image 892
BurninatorDor Avatar asked Dec 08 '11 23:12

BurninatorDor


2 Answers

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.

like image 59
Bill Karwin Avatar answered Sep 22 '22 14:09

Bill Karwin


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.

like image 33
Teudimundo Avatar answered Sep 22 '22 14:09

Teudimundo