php - 如何使用 PHP 将大型 Excel 文件导入 MySql 数据库

标签 php mysql excel upload large-file-upload

我必须使用 php 将 excel 文件的数据上传到 MySQL 数据库。我找到了相关代码,但无法上传大文件。

谁能告诉我如何增加以下链接中提到的代码的最大文件大小限制:

http://www.9code.in/how-to-import-excel-file-to-mysql-database-using-php/

<!DOCTYPE html>
<?php 
include 'db.php';
include 'Excel/reader.php';
function uploadFile($fieldName, $fileType, $folderName, $name = "")
{
    $flg = 0;
    $MaxID = "";
    $ext = "";
    $uploadfile = "";
    if (isset($fieldName) AND $fieldName['name'] != '')
    {
        $flg = 1;
        $allowed_filetypes = $fileType;
        // I Need to increase this..... I tried changing values but nothing happened
        $max_filesize = 1048576;     
        $filename = $fieldName['name'];
        if ($name == "")
            $MaxID = time() . time() . rand(1, 100);
        else
            $MaxID = $name;
        $ext = substr($filename, strpos($filename, '.'), strlen($filename) - 1);
        if($ext==".xlsx")
            $ext=".xls";
        if (!in_array($ext, $allowed_filetypes))
            echo "<h1>The file you attempted to upload is not allowed...</h1>";
        else if (filesize($fieldName['tmp_name']) > $max_filesize)
            echo "<h1>The file you attempted to upload is too large...</h1>";
        else 
        {
            $uploadfile = $folderName . "/" . $MaxID . $ext;
            if (move_uploaded_file($fieldName['tmp_name'], $uploadfile) == FALSE)
            {
                echo "<h1>Error in Uploading File...</h1>";
                $MaxID = "";
            }
            else
                $MaxID = $MaxID . $ext;
        }
    }
    return $MaxID;
}
if(isset($_POST['submit']))
{
    if($_FILES['csvFile']['name']!="")
    {
        $fileName=uploadFile($_FILES['excelFile'],array(".csv"),"excel_file");
        $row=0;
        if(($handle = fopen("excel/".$fileName , "r")) !== FALSE) 
        {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
            {
                $num = count($data);
                //print_r($data);
                $query="INSERT INTO StudentData(FirstName,LastName,MobileNo,City)VALUES('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."')";
                mysql_query($query);
            }
            fclose($handle);
        }
    }
    else if($_FILES['excelFile']['name']!="")
    {
        $fileName=uploadFile($_FILES['excelFile'],array(".xls",".xlsx"),"excel_file");
        $data = new Spreadsheet_Excel_Reader();
        $data->read('excel_file/'.$fileName);
        for($i=1;$i<=$data->sheets[0]['numRows'];$i++)
        {
            $firstname=$data->sheets[0]['cells'][$i][1];
            $lastname=$data->sheets[0]['cells'][$i][2];
            $mobile=$data->sheets[0]['cells'][$i][3];
            $city=$data->sheets[0]['cells'][$i][4];
            $query="INSERT INTO StudentData(FirstName,LastName,MobileNo,City)VALUES('".$firstname."','".$lastname."','".$mobile."','".$city."')";
            mysql_query($query);
        }
    }
}
if(isset($_POST['delete']))
{
    mysql_query("DELETE FROM StudentData");
}
?>

最佳答案

您可以在 MySQL 中使用 LOAD DATA 命令:Read More

你必须在 mysql 语句中使用加载数据。这可以将您的大文件加载到数据库中。

mysqli_query($dblink, '
    LOAD DATA LOCAL INFILE "'.$file.'"
        INTO TABLE transactions
        FIELDS TERMINATED by ","
        OPTIONALLY ENCLOSED BY "\'"
        LINES TERMINATED BY "\n"
');

关于php - 如何使用 PHP 将大型 Excel 文件导入 MySql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23869743/

相关文章:

php - 从整数生成伪随机 6 字符字符串

带数组的 PHP getter 和 setter

php - 如何修复 SQLSTATE[42000] 错误;使用准备好的语句

jquery - 当盒子充满 jquery .post() 时从 mysql 获取行

php - Mysqli 弄乱了我的 while 循环

MySQL错误1452(23000): Cannot add or update a child row: a foreign key constraint fails Another issue

java - 如何对 jxls 模板中的非连续单元格求和

java - 使用 Apache POI 将父列添加到 Excel 数据透视表

php - 最好使用私有(private)方法还是保护方法?

mysql - 是什么导致vba应用程序后端MYSQL超时