Back to blog

PHP and MySQL LOAD_FILE solution


Are you near your boiling point when using the MySQL LOAD_FILE function fromĀ  a PHP script, here is the solution.
You already debugged the file uploads, file movements, renaming and so on, nothing worked.

Sample query

$sql = "INSERT INTO myFiles (id, thefile) VALUES ('1', LOAD_FILE('/tmp/somefile'))";

The above query might give you a headache because it appears to be ok, and guess what, it is!
Me and my colleague were repeatedly struggling with the fact the query didn’t worked from PHP but did work from our MySQL manager tool.

I do hear you thinking, huh, how is that possible? Well we’re using the user ‘root’ in our manager tool which have all privileges of course. Now some of you will have a blinking light now.

You must give the user you use to connect to your MySQL database the FILE privileges.

It seems so simple, but the majority of the MySQL users doesn’t have this by default.

GRANT FILE ON *.* TO 'someuser'@'somehost';
FLUSH PRIVILEGES;

Done!

Labels: mysql php troubleshooting

Drop a note