-1

I make a admin page to upload the csv file into mysql. The file csv has size 59,9mb. But my script didn't upload the file into mysql. Would you mind to help me to fix this problem? Here is my code :

<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>

I need to fix this problem as soon as possible. Please Help. Thank You.

4

2 回答 2

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");
}
于 2012-06-23T01:47:33.987 回答
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';
    }

}



?>
于 2012-06-23T01:04:03.750 回答