excel文件过大怎么导入mysql,如何使用PHP将大型Excel文件导入MySql数据库

I have to upload excel file's data to MySQL database using php. I have found the code for that but I am unable to upload large files.

Can anyone please tell me that how can I increase the max file size limit for the code mentioned in below link:

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 "

The file you attempted to upload is not allowed...

";

else if (filesize($fieldName['tmp_name']) > $max_filesize)

echo "

The file you attempted to upload is too large...

";

else

{

$uploadfile = $folderName . "/" . $MaxID . $ext;

if (move_uploaded_file($fieldName['tmp_name'], $uploadfile) == FALSE)

{

echo "

Error in Uploading File...

";

$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");

}

?>

解决方案

According to ASNAOUI Ayoub I made the following changes:

; Maximum allowed size for uploaded files.

upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize

post_max_size = 40M

But Stil the Problem was same then I tried to change the

$max_filesize = 41943040

Now It perfectly works.....

Thanks Everyone for the help

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值