我想知道如何使用PHP从大型CSV或XLS文件上传和检索数据。有没有可用的PHP库?
我尝试使用下面提到的代码,但上传需要花费大量时间。是唯一可以通过使用cron来完成的方法,还是有另一种方法?
$file=fopen(base_url()."/xml/sample.csv","r");
while(! feof($file))
{
pr(fgetcsv($file));
}
fclose($file);
答案 0 :(得分:1)
运行脚本的一种方法似乎是最好的方法
$this->db->query("LOAD DATA LOCAL INFILE '".base_url()."/xml/sample.csv'
REPLACE INTO TABLE TABLE_NAME FIELDS TERMINATED BY ','
ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES");
运行脚本的另一种方法是
<?php
$row = 1;
if (($handle = fopen("ptt.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row == 1){ $row++; continue; }
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if(strpos($data[$c], 'Finished') !== false) {
$c++;
echo "<TR> <TD nowrap>" . $data[$c] . "</ TD>"; }
Else{
echo "<TD nowrap>" . $data[$c] . "</ TD>";
}
}
}
fclose($handle);
}
?>
答案 1 :(得分:0)
<?php
ini_set('max_execution_time',0);
$no = 0;
//validate whether uploaded file is a csv file
$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');
//$csvMimes = array('application/x-csv', 'text/x-csv', 'text/csv', 'application/csv');
if (!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $csvMimes)) {
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
//open uploaded csv file with read only mode
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
//skip first line
fgetcsv($csvFile);
//parse data from csv file line by line
while (($line = fgetcsv($csvFile)) !== FALSE) {
if ($line[0] != '') {
//check whether member already exists in database with same email
$sql = "SELECT * FROM `parts_master` WHERE PART_NUM = '$line[1]'";
$res = mysqli_query($conn, $sql);
$num = mysqli_num_rows($res);
if ($num != 0) {
$row = mysqli_fetch_array($res);
$row_id = $row['id'];
$sql2 = "UPDATE `parts_master` SET ROOT_PART_NUM = '$line[2]', PART_DESC = '$line[3]', MRP = '$line[4]', ISSUE_INDICATOR = '$line[5]', TAX_DESC = '$line[6]', HS_CODE = '$line[7]' WHERE id = $row_id";
} else {
$sql2 = "INSERT INTO `parts_master` (id, PART_NUM, ROOT_PART_NUM, PART_DESC, MRP, ISSUE_INDICATOR, TAX_DESC, HS_CODE, isdeleted)
VALUES (NULL ,'$line[1]','$line[2]','$line[3]','$line[4]','$line[5]','$line[6]','$line[7]',0)";
}
$res2 = mysqli_query($conn, $sql2);
if (!$res2) {
$no++;
}
}
}
//close opened csv file
fclose($csvFile);
}
} else {
$no++;
}
if ($no != 0) {
echo 'error';
} else {
echo 'success';
}
?>
&#13;