Name

LOAD_FILE( )

Synopsis

LOAD_FILE(filename)

This function reads the contents of a file. The user must have file privileges in MySQL, and the file must be readable by all users on the filesystem. It returns NULL if the file doesn’t exist, if the user doesn’t have proper permissions, or if the file is otherwise unreadable.

UPDATE applications
SET essay = LOAD_FILE('/tmp/smith_john.txt')
WHERE applicant_id = '7382';

In this example, an essay written by someone who is applying for admission to the university is loaded into the essay column (which is a BLOB datatype) of the row for the applicant in the applications table. The entire contents of the file, including any binary data such as hard returns and photographs, are loaded from the file into the table.

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.