Wednesday, September 28, 2011

MySql LOAD_FILE Function

LOAD_FILE(file_name_and_complete_path)

It’s built in MySql String function that reads a file from server host and returns the contents of file as string. If file can’t read or doesn’t exist then this function will return NULL.

Important Points:

  • è Must pass the full path name to the file as parameter
  • è User must have the FILE privilege
  • è File must be readable
  • è Size of file must be less than max_allowed_packet in bytes. Default is (1048576 bytes). Its range is 1024 – 1073741824
  • è If system variable named secure_file_priv is set to a non empty directory, then the file must be located in the directory specified by this system variable. Its advisable to set this variable to the source directory for your file, otherwise there is a risk for LOAD_FILE Injection. If you want to check the current value for this system variable you can simply use the command:
mysql>SELECT @@secure_file_priv;

Example:

Mysql> UPDATE SET blob_column_name = LOAD_FILE(‘/tmp/picture’) WHERE id = 1;

No comments: