📜  在 php 代码示例中上传 csv 文件

📅  最后修改于: 2022-03-11 14:54:18.292000             🧑  作者: Mango

代码示例2
if (isset($_POST['import'])) {
    if ($_FILES['file']['name']) {
        $filename = explode(".", $_FILES['file']['name']);
        if ($filename[1] == 'csv') {
            $handle = fopen($_FILES['file']['tmp_name'], "r");
           $i = 0;
            while ($getData = fgetcsv($handle)) {
                if($i > 0){
                    $productName   = $getData[0];
                    $productDesc   = $getData[1];
                    $productQty   = $getData[2];
                    $productPrice   = $getData[3];
                    $productTotal   = $getData[4];
                    $sql = "INSERT INTO import (productName,productDesc,productQty,productPrice,productTotal) VALUES('" . $getData[0] . "','" . $getData[1] . "','" . $getData[2] . "','" . $getData[3] . "','" . $getData[4] . "')";
                    $result = mysqli_query($conn, $sql);
                    header("Location:import.php");
                }
                $i++;
            }
            fclose($handle);
        }
    }
}

Check your table name,column name & where you want to redirect using header function.