SQL查询和临时文件权限
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 '\\
' 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.
我正在尝试使用HTML / PHP在MySQL数据库中导入.csv文件。 代码是: p>
HTML p>
&lt; form method =“post”action =“”enctype =“multipart / form-data “&gt;
文件:&lt; input type =”file“name =”csv“&gt;
&lt; input type =“submit”value =“发送”name =“subCSV”&gt;
&lt; / form&gt;
code> pre>
PHP p>
$ file = $ _FILES [“csv”] [“tmp_name”];
try {
$ query = $ connexion - &gt; 准备(“LOAD DATA INFILE?into TABLE myTable FIELDS TERMINATED BY';'LINIINATED by'\
\
'IGNORE 1 ROWS”);
$ query-&gt; bindValue(1,$ file,PDO :: PARAM_STR);
$ query-&gt; execute();
} catch(PDOException $ e){echo $ e-&gt; getMessage(); $ errorCode = $ e-&gt; getCode();
code> pre>
但是我收到了这个错误: p>
找不到文件'/ tmp / phpl8eY59'(错误代码:13)
code> pre>
明确指定文件有效。 权限很好,运行Apache的用户和文件所有者是相同的。 我已经授予数据库用户文件权限。 p>
显然配置AppArmor可以解决问题,但我没有使用它,因为它没有安装在我的服务器上。 p>
div>
I've solved the problem with this:
chmod($file, 0644);
The Apache/PHP user and the MySQL user were not the same.
You could try using "LOAD DATA LOCAL INFILE". See eg the answer here: LOAD DATA INFILE does not work. The problem is that there seem to be various causes of this problem.