Search code examples
phphtmlmysqlload-data-infile

SQL query and temporary file permissions


I'm trying to import a .csv file in a MySQL database using HTML/PHP. The code is:

HTML

<form method="post" action="" enctype="multipart/form-data">
  File: <input type="file" name="csv"> 
  <input type="submit" value="Send" name="subCSV">
</form>

PHP

$file = $_FILES["csv"]["tmp_name"];

try {
  $query = $connexion -> prepare("LOAD DATA INFILE ? into TABLE myTable FIELDS TERMINATED BY ';' LINES TERMINATED BY '\\r\\n' IGNORE 1 ROWS");
  $query->bindValue(1, $file, PDO::PARAM_STR);
  $query->execute();
} catch(PDOException $e) { echo $e->getMessage(); $errorCode = $e->getCode();

But I'm getting that error:

File '/tmp/phpl8eY59' not found (Errcode: 13)

Specifying the file explicitly works. The permissions are fine, the user running Apache and the file owner are identical. I have granted file permission on the database user.

Apparently configuring AppArmor would solve the problem, but I'm not using it, as it is not installed on my server.


Solution

  • I've solved the problem with this:

    chmod($file, 0644);
    

    The Apache/PHP user and the MySQL user were not the same.