无法将csv导入数据库mysql

时间:2012-06-23 00:24:22

标签: php mysql csv import

我创建了一个管理页面,将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>

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

2 个答案:

答案 0 :(得分:0)

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 :(得分:0)

您是否可以通过命令行使用该导入命令将文件导入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");
}