Whilst trying to export some data to a file from MySQL using phpmyadmin with the SELECT * INTO OUTFILE command, I received the following error:

#1 - Can't create/write to file '/path/to/file' (Errcode: 13)

After messing around with chmod and chown for a bit I started to search the internet. The error wasn't caused by the MySQL tmpdir variable either - it was apparmor. To resolve this error I had to edit the apparmor config file for MySQL and restart the MySQL service as follows:

Edit the config file

sudo nano /etc/apparmor.d/usr.sbin.mysqld

Following the same syntax as the other entries, add a line for your specific directory

/path/to/the/specific/directory/you/want/to/save/file/in/* w,

Restart MySQL

sudo service mysql restart

NOTE: The directory permissions also needed for this to work were set to 755 and with the owner/group set to 'mysql'.