Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How To Import Large Excel File To MySql Database Using PHP

Featured Replies

I have to upload excel file's data t0 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 below.

I have also attached the source files.

 

 

<?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 want to increase the Max File Size.... I Tried increasing the below value but it had no effect.
            $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");
	}
?>

 

excelToSql.zip

  • Author

In my php.ini i have written the below lines:

 

; 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 still cant upload files greater than 1 MB


Please download the zip file and see whether changes to be done are in the code....

What error are you getting? Is the service timing out? Is it saying that the file is too big to upload?

  • Author

Is it saying that the file is too big to upload

  • Author

Yes.. Now its working fine.....


Thanks Everyone

  • 3 weeks later...

You need to set the value of upload_max_filesize and post_max_size in your php.ini :



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

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

I have to add that you have to restart the server


Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.