Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devkon98's avatar

How to upload excel to database using plain PHP

Hello im using plain php to make a simple excel import to database, the values wont import. This is my code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Upload Excel File</title>
</head>
<body>
    <h1>Upload Excel File</h1>
    <form method="post" action="upload.php" enctype="multipart/form-data">
        <input type="file" name="file">
        <br><br>
        <input type="submit" value="Upload">
    </form> 
</body>
</html>

And this is the php:

<?php
require('db_config.php');
// Check if a file was uploaded
if (isset($_FILES["file"]) && $_FILES["file"]["error"] == 0) {
    // Define allowed file extensions
    $allowedExtensions = array("xlsx", "xls");

    // Get file extension
    $extension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);

    // Check if the file extension is allowed
    if (in_array($extension, $allowedExtensions)) {
        // Define the path to save the uploaded file
        $filePath = "uploads/" . $_FILES["file"]["name"];

        // Move the uploaded file to the defined path
        move_uploaded_file($_FILES["file"]["tmp_name"], $filePath);

        // Open the uploaded Excel file
        $objPHPExcel = PHPExcel_IOFactory::load($filePath);

        // Get the first worksheet in the Excel file
        $worksheet = $objPHPExcel->getActiveSheet();

        // Loop through the rows in the worksheet
        foreach ($worksheet->getRowIterator() as $row) {
            // Get the cell values in the current row
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(false);
            $cells = array();
            foreach ($cellIterator as $cell) {
                $cells[] = $cell->getValue();
            }

            // Insert the row data into the database
            $sql = "INSERT INTO leaderboard (AFFILIATOR_NAME, TOP_N, USER_ID,USERNAME,WAGERED) VALUES ('".$cells[1]."', '".$cells[2]."', '".$cells[3]."','".$cells[4]."','".$cells[5]."')";
            // Execute the SQL statement
            // ...

        }

        // Delete the uploaded Excel file
        unlink($filePath);

        // Redirect the user to the success page
        //header("Location: success.php");
		echo 'Values inserted successfully!';
        //exit;
    } else {
        // Invalid file extension
        //header("Location: error.php");
		echo 'Values failed to insert!';
       /// exit;
    }
} else {
    // No file uploaded
    echo 'No file uploaded!';
}
?>
0 likes
1 reply
LaryAI's avatar
Level 58

The code seems to be missing the PHPExcel library. You can download it from the official website and include it in your project. Here's an example of how to include it:

require_once 'PHPExcel/Classes/PHPExcel.php';

Also, make sure that the file path is correct and that the file is being uploaded to the server. You can check this by adding the following code after the move_uploaded_file function:

if (file_exists($filePath)) {
    echo "File uploaded successfully.";
} else {
    echo "File upload failed.";
}

Finally, make sure that the SQL statement is correct and that the table and column names match your database. You can also add some error handling to the SQL statement to catch any errors that may occur. Here's an example:

if ($conn->query($sql) === TRUE) {
    echo "Record inserted successfully.";
} else {
    echo "Error inserting record: " . $conn->error;
}

Please or to participate in this conversation.