无法使用“ PHP” Ajax导入完整的“ CSV”行

时间:2019-10-01 02:47:59

标签: php mysql ajax

我正在使用getcsv将csv导入数据库。我有大量csv格式的数据表。目前,我正在导入具有53,000行和35列的csv。当我降低列数时,它会导入数据库中的大约所有行,但是当我要将csv的所有列导入到mysql表中时,它就不会导入。

我的csv具有所有类型的字符,例如char,数字,符号,特殊字符等

请查看我的代码:

Index.php:

 <form action="upload.php" method="post" enctype="multipart/form-data">                                                                                                                                             <input type="text" name="type2" Placeholder='Type Folder Name' required >                                                                                           <input type="file" name="csv">
<input type="submit" name="submit" value="Import" class="btn btn-success waves-effect waves-light"/>
   </form>

UPLOAD.php

<script>
//Declaration of function that will insert data into database
 function senddata(filename){
        var file = filename;
        $.ajax({
            type: "POST",
            url: "senddata.php",
            data: {file},
            async: true,
            success: function(html){
                $("#result").html(html);
            }
        })
        }
 </script>


$csv = array();
$batchsize = 1000; //split huge CSV file by 1,000, you can modify this based on your needs
if($_FILES['csv']['error'] == 0){
    $name = $_FILES['csv']['name'];
    $ext = explode('.', $_FILES['csv']['name']);
    $file_ext=strtolower(end($ext));

    $tmpName = $_FILES['csv']['tmp_name'];
    if($file_ext === 'csv'){ //check if uploaded file is of CSV format
        if(($handle = fopen($tmpName, 'r')) !== FALSE) {
            set_time_limit(0);
            $row = 0;
            while(($data = fgetcsv($handle)) !== FALSE) {
                $col_count = count($data);
                //splitting of CSV file :
                if ($row % $batchsize == 0):
                    $file = fopen("minpoints$row.csv","r");
                endif;
                $csv[$row]['pno'] = $data[0];
                $csv[$row]['area'] = $data[1];
                $csv[$row]['project'] = $data[2];
                $csv[$row]['flatno'] = $data[3];
                $csv[$row]['balcony'] = $data[4];
                $csv[$row]['parking'] = $data[5];
                $csv[$row]['common_area'] = $data[6];
                $csv[$row]['floor'] = $data[7];
                $csv[$row]['rooms'] = $data[8];
                $csv[$row]['shop'] = $data[9];
                $csv[$row]['flats'] = $data[10];
                $csv[$row]['offices'] = $data[11];
                $csv[$row]['category'] = $data[12];
                $csv[$row]['total_area'] = $data[13];
                $csv[$row]['plot_no'] = $data[14];
                $csv[$row]['emirates'] = $data[15];
                $csv[$row]['name'] = $data[16];
                $csv[$row]['area_owned'] = $data[17];
                $csv[$row]['address'] = $data[18];
                $csv[$row]['phone'] = $data[19];
                $csv[$row]['email'] = $data[20];
                $csv[$row]['fax'] = $data[21];
                $csv[$row]['pobox'] = $data[22];
                $csv[$row]['gender'] = $data[23];
                $csv[$row]['dob'] = $data[24];
                $csv[$row]['mobile'] = $data[25];
                $csv[$row]['smobile'] = $data[26];
                $csv[$row]['passport'] = $data[27];
                $csv[$row]['issue_date'] = $data[28];
                $csv[$row]['expiry_date'] = $data[29];
                $csv[$row]['poissue'] = $data[30];
                $csv[$row]['emirates_no'] = $data[31];
                $csv[$row]['emirates_exp'] = $data[32];
                $csv[$row]['residence'] = $data[33];
                $csv[$row]['nationality'] = $data[34];
                $csv[$row]['type2'] = $_POST['type2'];

                $min = $data[0];
                $points = $data[1];
                $points2 = $data[2];
                $points3 = $data[3];
                $points4 = $data[4];
                $points5 = $data[5];
                $points6 = $data[6];
                $points7 = $data[7];
                $points8 = $data[8];
                $points9 = $data[9];
                $points10 = $data[10];
                $points11 = $data[11];
                $points12 = $data[12];
                $points13 = $data[13];
                $points14 = $data[14];
                $points15 = $data[15];
                $points16 = $data[16];
                $points17 = $data[17];
                $points18 = $data[18];
                $points19 = $data[19];
                $points20 = $data[20];
                $points21 = $data[21];
                $points22 = $data[22];
                $points23 = $data[23];
                $points24 = $data[24];
                $points25 = $data[25];
                $points26 = $data[26];
                $points27 = $data[27];
                $points28 = $data[28];
                $points29 = $data[29];
                $points30 = $data[30];
                $points31 = $data[31];
                $points32 = $data[32];
                $points33 = $data[33];
                $points34 = $data[34];
                $points35 = $_POST['type2'];

                $json = "'$min', '$points', '$points2','$points3','$points4','$points5','$points6','$points7','$points8','$points9','$points10','$points11' ,'$points12','$points13' ,'$points14','$points15','$points16','$points17','$points18','$points19','$points20','$points21','$points22','$points23','$points24','$points25','$points26','$points27','$points28','$points29','$points30','$points31','$points32','$points33','$points34','$points35'";
                fwrite($file,$json.PHP_EOL);
                //sending the splitted CSV files, batch by batch...
                if ($row % $batchsize == 0):
                    echo "<script> senddata('minpoints$row.csv'); </script>";
                endif;
                $row++; 
            }
            fclose($file);
            fclose($handle);
        }
    }
    else
    {
        echo "Only CSV files are allowed.";
    }
    //alert once done.
    echo "<script> alert('Data imported!');</script>";
}

Senddata.php

include('connect.php');
 $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');

$data = $_POST['file'];
$handle = fopen($data, 'r');
$test = file_get_contents($data , $csvMimes);
if ($handle) {
    $counter = 0;
    //instead of executing query one by one,
    //let us prepare 1 SQL query that will insert all values from the batch
    $sql ="INSERT INTO imported_property(pno, area, project, flatno, balcony, parking, common_area, floor, rooms, shop, flats, offices, category, total_area, plot_no, emirates, name, area_owned, address, phone, email, fax, pobox, gender, dob, mobile, smobile, passport, issue_date, expiry_date, poissue, emirates_no, emirates_exp, residence, nationality,type2) VALUES ";

    while (($line = fgets($handle)) !== false) {
      $sql .= "($line),";
      $counter++;
    }
    $sql = substr($sql, 0, strlen($sql) - 1);
     if ($conn->query($sql) === TRUE) {
    } else {
     }
    fclose($handle);
} else {  
} 
//unlink CSV file once already imported to DB to clear directory
unlink($data);

0 个答案:

没有答案