嗨,我非常需要你的帮助。
使用mysql加载数据infile导入.CSV文件后显示错误。
我的表格上传工作正常
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
和使用load data infile的PHP上传脚本。
require("../config/conn.php");
if (is_uploaded_file($_FILES['my-file']['tmp_name']) && $_FILES['my-file']['error']==0) {
$path = 'C:/xampp/htdocs/dom/test/uploads/' . $_FILES['my-file']['name'];
if (!file_exists($path)) {
if (move_uploaded_file($_FILES['my-file']['tmp_name'], $path)) {
echo $mysql = "LOAD DATA LOCAL INFILE '".$_FILES['my-file']['name']."'
REPLACE INTO TABLE table
FIELDS
TERMINATED BY ','
LINES
TERMINATED BY '\\n'
IGNORE 1 LINES
(`col1`,`col2`,`col3`,`col4`,`col5`....)";
$query = mysqli_query($link, $mysql) or die(mysqli_error($link));
if(!$query)
{
printf("Error message: %s\n", mysqli_error($link));
}
} else {
echo "The file was not uploaded successfully.";
}
} else {
echo "File already exists. Please upload another file.";
}
} else {
echo "The file was not uploaded successfully.";
echo "(Error Code:" . $_FILES['my-file']['error'] . ")";
}
一切都很好,除了加载数据本地infile无法看到正确的路径。请查看错误:无法找到文件'logJan262013.CSV'。,但.csv文件已在文件夹'uploads /'中成功上传。任何帮助将不胜感激。
非常感谢!
答案 0 :(得分:1)
您正在使用查询:
$mysql = "LOAD DATA LOCAL INFILE '".$_FILES['my-file']['name']."'...
我相信你希望使用完整的路径
$mysql = "LOAD DATA LOCAL INFILE '".$path."'...
答案 1 :(得分:0)
你做
if (move_uploaded_file($_FILES['my-file']['tmp_name'], $path)) {
之前
$query = mysqli_query($link, $mysql)
因此,LOAD DATA
找不到文件也就不足为奇了。
如果您为LOAD DATA
提供正确的$path
,则应该按预期工作。
答案 2 :(得分:0)
你还没告诉LOAD DATA INFILE路径!尝试告诉它的路径,一切都应该没问题。