Is There An Easy Way To Upload Excel Data Directly To The Database?

Asked by Anonymous on 02 Feb 2018 2301 Views

Anonymous on 03 Feb 2018

Laravel Excel is quite helpful is resolving that one but we've got easy file handling php functions that can do the work with much convenience. Let's have a look at it.

public function importExcel()
    $file = $_FILES['uploads']['tmp_name']; 
    $handle = fopen($file, "r"); 

    while($filesop = fgetcsv($handle, 1000, ",")){ 
        $name = $filesop[0]; 
        $gender = $filesop[1]; 
        $address = $filesop[2]; 
        $age = $filesop[3]; 
        $education = $filesop[4]; 
        $contact = $filesop[5]; 


At first, we retrieved the file from the temporary location and we opened it in reading mode.

Using the while loop, we stored the values from the excelsheet rows as indexed arrays to some variables.

Finally, we added those variables in the insert query and executed while remaining inside the loop. That makes the function run until the last row of excelsheet and insert each of those rows individually in database.

Once completed, you can store a success message in session and display it to the user while redirecting the user.

You must be logged in to answer a question. or