如何进口大量的Excel文件MySQL数据库使用PHP(How To Import Large Ex

2019-10-20 04:35发布

我不得不使用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");
}
?>

Answer 1:

您可以使用LOAD DATA在MySQL命令: 更多

你必须使用MySQL中的语句加载数据。 这可以在数据库中装载的大型文件。

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


Answer 2:

看看这些值在php.ini

的upload_max_filesize = 10M的post_max_size = 10M



Answer 3:

你需要设置你的php.ini的upload_max_filesize和post_max_size要的价值:

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

我要补充一点,你不得不重新启动服务器



Answer 4:

据ASNAOUI阿尤布我做了以下修改:

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

但史迪威的问题是相同的,然后我试图改变

$max_filesize = 41943040

现在,它完美的作品.....

感谢大家的帮助



文章来源: How To Import Large Excel File To MySql Database Using PHP