【问题标题】:cannot import csv to database mysql无法将 csv 导入数据库 mysql
【发布时间】:2012-06-23 00:24:22
【问题描述】:

我创建了一个管理页面来将 csv 文件上传到 mysql。文件 csv 的大小为 59.9mb。但是我的脚本没有将文件上传到mysql。你介意帮我解决这个问题吗?这是我的代码:

<HTML>
<HEAD>
<TITLE> Admin Update </TITLE>
</HEAD>

<BODY>
<form enctype='multipart/form-data' action="<?php echo $PHP_SELF ?>" method='post'>
<font face=arial size=2>Type file name to import:</font><br>
<input type='file' name='filename' size='20'><br>
<input type='submit' name='submit' value='submit'></form>
<?php
include("phpsqlajax_dbinfo.php");
$connection = mysql_connect ('127.0.0.1', $username, $password);
if (!$connection) {  die('Not connected : ' . mysql_error());} 

$db_selected = mysql_select_db("ipcoba", $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
} 
if(isset($_POST['submit']))
{
// address to copy csv file
$target_path = "C:/xampp/htdocs/bikinwebtracert";     

$target_path = $target_path . basename( $_FILES['filename']['name']);

if(move_uploaded_file($_FILES['filename']['tmp_name'], $target_path)) {
echo "<font face=arial size=2>The file ". basename( $_FILES['filename']['name']). " Upload Success</font><br>";
} else{
echo "<font face=arial size=2>Failed to Upload, Please try again</font><br>";
}

$filename=$target_path;
$load=mysql_query("LOAD DATA LOCAL INFILE $filename INTO TABLE CityBlocks FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 2 LINES (startIpNum, endIpNum, locId)'\n' ") or die(mysql_error());
if ($load){
echo("Failed");
} else {
echo("Success");
}
}
?>
</BODY>
</HTML>

我需要尽快解决这个问题。请帮忙。谢谢。

【问题讨论】:

  • 是的,我们需要查看您遇到的错误。但是我的第一个猜测是,您只需将 post_max_size 的 ini 设置增加到默认值 8M 以上,这样您的 60M 文件就可以通过了。
  • 没有错误。但是文件没有上传。有什么问题吗?
  • @RyanLaBarre,我已经更改了 php.ini 中的默认大小。但它仍然没有工作。如何解决这个问题?
  • 首先隔离错误:究竟什么不起作用?你的代码有多远一切正常?文件上传了吗?当您知道错误发生在哪一行时,找出问题所在并修复它!确保您启用了错误日志记录。
  • 在检查提交后(“//提交 csv 的地址”附近),添加 print_r($_FILES) - 将该输出添加到您的问题中。

标签: php mysql csv import


【解决方案1】:

1 - 尝试改进您的过滤器,以检测错误发生的位置

2 - 尝试为 csv 文件使用一个名称(将上传的文件重命名为 test.csv),以保护自己,并确保文件将成功上传

3 - 尝试使用 file_get_contents 而不是 move_uploaded_file

4 - 确定错误发生的时间?上传或导入错误?

试试这个代码:

编辑

很高兴您解决了问题

我已经尽可能解释了,对不起我的英语

<?php

/*
The script idea is
1- we get the file name on the user computer
2- we rename it to import.csv
3- we check if file already exists or not
4- we upload the file
5- import the file
*/

/*
STEP 1
if there is incoming files.
*/
if(!$_FILES['filename']['tmp_name']||empty($_FILES['filename']['tmp_name']))
    die('Please select a file');
if($_FILES['filename']['tmp_name']){
/*
STEP 2
Here we will check if there is import.csv file
if there is import.csv file, that's mean there is a process underway
*/
    if(file_exists('/tmp/import.csv'))
        die('Please wait, we still working on the previous process...');
/*
File path
on the user computer
*/
    $filepath=$_FILES['filename']['tmp_name'];
/*
New path for the file
/tmp folder.. always have a permits that allow the lifting of the file
*/
    $newpath='/tmp/import.csv';
/*
Get the content of the file
read more about file_get_contents()
*/
    $getFile=file_get_contents($filepath);
/*
file_put_contents()
function that's requires to parametrs #1 file name #2 file content
and it will create a file with the name and content that you have provided
*/
    $uploade=(!empty($getFile))?file_put_contents($newpath,$getFile):die('We cant get the file!');
/*
if file exists we have succeed
*/
    if(!file_exists($newpath))
        die('The file did not uploaded..');

    //import to database
    $load = mysql_query("LOAD DATA LOCAL .........");
    if($load){
        //remove the file..
        unlink($newpath);
        echo 'success...';
    }

    else{
        echo(file_exists($getFile))?'File uploaded but did not imported to mysql':'File not uploaded';
    }

}



?>

【讨论】:

  • 感谢您的回答,但我不太了解您的 1-4 步骤。你能一步一步给我解释一下吗?我是新手,我有点难以理解。谢谢。
  • 对于“'/tmp/import.csv'”,import.csv 是我尝试上传的文件吗?然后我可以用变量 $filename 替换它,就像我在查询加载数据中发布上面的问题一样?如果我对你的真实解释有误解,我很抱歉。
【解决方案2】:

您是否可以使用该导入命令通过命令行将文件导入mysql?在调查其余代码之前先尝试确保它正常工作。

编辑:

根据mysql_query,如果成功,它应该返回 TRUE 或成功的资源,如果不成功则返回 FALSE。你的代码有这个:

if ($load){
    echo("Failed");
} else {
    echo("Success");
}

我认为可能存在逻辑错误。尝试将代码更改为此并告诉我们会发生什么:

$load=mysql_query("LOAD DATA LOCAL INFILE $filename INTO TABLE CityBlocks FIELDS  TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 2 LINES (startIpNum, endIpNum, locId)'\n' ");
if (!$load){
    echo mysql_error();
} else {
echo("Success");
}

【讨论】:

  • 我从未尝试通过命令行上传。但是我尝试通过 phpmyadmin 使用查询“加载本地数据...”来上传它,它已经完成了。但是当我尝试制作我上面发布的界面时。该文件无法上传和导入。有什么建议吗?
  • 好吧,当负载运行时,它有 mysql_error()。打印出来的是什么?
  • 很抱歉问这个问题。我必须在哪里运行 mysql_error?在 php 或 phpmyadmin (mysql) 中?如果我在 phpmyadmin 中运行 mysql_error,则没有错误。成功导入表。
  • 在 php 中也没有 mysql_error。但数据没有上传并导入到表中。这怎么可能解决?谢谢。
  • 你需要逐行调试它。验证它正在连接到数据库,它正在使用正确的数据库,文件路径是否正确,加载命令是否正确执行。 Xdebug 可能对你有帮助。否则,您基本上必须回显变量以确保它们是您想要的,执行该行并检查输出。使用 echo 打印并 die 停止脚本并逐行执行此操作,直到您隔离错误。这很耗时,但您应该很快就能找到它。
猜你喜欢
  • 2019-10-17
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 2017-11-15
  • 2012-06-29
  • 1970-01-01
  • 2017-11-13
相关资源
最近更新 更多