Home > Backend Development > PHP Tutorial > How to implement the employee attendance data import function through PHP?

How to implement the employee attendance data import function through PHP?

WBOY
Release: 2023-09-26 18:30:01
Original
919 people have browsed it

How to implement the employee attendance data import function through PHP?

How to implement the employee attendance data import function through PHP?

Introduction:
With the development of informatization and the expansion of enterprise scale, employee attendance data management has become particularly important. By using PHP language, you can easily implement the import function of employee attendance data. This article will introduce in detail how to use PHP to implement the employee attendance data import function and provide specific code examples.

1. Determine the data format:
Before starting to write code, we first need to determine the format of employee attendance data. Generally, employee attendance data usually exists in the form of Excel or CSV files. In order to facilitate the import of data, we need to ensure that the data file is in the correct format and can be parsed.

2. Create an HTML page:
We first create an HTML page to implement the file upload function. In this page, insert a file upload form, for example:

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file">
    <input type="submit" value="提交" name="submit">
</form>
Copy after login

This form uses enctype="multipart/form-data", which is the encoding type used for file upload .

3. Create a PHP processing file:
Next, we need to create a PHP file to process the uploaded file. In the upload.php file, first we need to verify the file to ensure that the file is uploaded successfully and is in the file format we expect, for example:

<?php
if(isset($_POST['submit'])){
    $file = $_FILES['file'];
    
    // 检查文件上传是否成功
    if ($file['error'] > 0) {
        die('文件上传出错!');
    }
    
    // 检查文件格式是否正确
    $file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    if($file_ext != 'csv' && $file_ext != 'xls' && $file_ext != 'xlsx'){
        die('只允许上传csv、xls和xlsx文件!');
    }
    
    // 处理文件逻辑
    // ...
}
?>
Copy after login

In the above code, we Obtain the uploaded file information through $_FILES['file'], and check whether the file upload is successful through $file['error']. Then, we use the pathinfo() function to obtain the file extension, and by judging the extension, ensure that the file format is csv, xls or xlsx.

4. Import data:
Next, we need to import the uploaded attendance data into the database. First, we need to choose the corresponding parsing method according to the type of data file. For CSV files, we can use the fgetcsv() function to read data line by line; for Excel files, we can use third-party libraries such as PHPExcel.

The following is a sample code for processing a CSV file using the fgetcsv() function:

<?php
require_once 'PHPExcel/PHPExcel.php'; //引入PHPExcel类文件

if(isset($_POST['submit'])){
    $file = $_FILES['file'];
    
    // 检查文件上传是否成功
    if ($file['error'] > 0) {
        die('文件上传出错!');
    }
    
    // 检查文件格式是否正确
    $file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    if($file_ext != 'csv' && $file_ext != 'xls' && $file_ext != 'xlsx'){
        die('只允许上传csv、xls和xlsx文件!');
    }
    
    // 处理文件逻辑
    if($file_ext == 'csv'){
        $handle = fopen($file['tmp_name'], 'r');
        
        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
            // 将数据插入数据库
            // ...
        }
        
        fclose($handle);
    }else{
        $objPHPExcel = PHPExcel_IOFactory::load($file['tmp_name']);
        $sheet = $objPHPExcel->getActiveSheet();
        
        foreach($sheet->getRowIterator() as $row){
            $rowData = array();
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(FALSE);
            
            foreach($cellIterator as $cell){
                $rowData[] = $cell->getValue();
            }
            
            // 将数据插入数据库
            // ...
        }
    }
}
?>
Copy after login

In the above code, we used fopen() The function opens the CSV file and reads the data line by line using the fgetcsv() function. For Excel files, we first use the PHPExcel class to load the Excel file, then get each row of data through an iterator and insert the data into the database.

5. Insert data into the database:
Finally, we need to write code to insert the read attendance data into the database. Here, we take the MySQL database as an example, assuming that a table for storing attendance data has been created in the database.

The following is a sample code for inserting data into the database:

<?php
require_once 'PHPExcel/PHPExcel.php'; //引入PHPExcel类文件

if(isset($_POST['submit'])){
    $file = $_FILES['file'];
    
    // 检查文件上传是否成功
    if ($file['error'] > 0) {
        die('文件上传出错!');
    }
    
    // 检查文件格式是否正确
    $file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    if($file_ext != 'csv' && $file_ext != 'xls' && $file_ext != 'xlsx'){
        die('只允许上传csv、xls和xlsx文件!');
    }
    
    // 处理文件逻辑
    if($file_ext == 'csv'){
        $handle = fopen($file['tmp_name'], 'r');
        
        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
            // 将数据插入数据库
            $name = $data[0];
            $time = $data[1];
            
            // 使用SQL语句将数据插入数据库
            // ...
        }
        
        fclose($handle);
    }else{
        $objPHPExcel = PHPExcel_IOFactory::load($file['tmp_name']);
        $sheet = $objPHPExcel->getActiveSheet();
        
        foreach($sheet->getRowIterator() as $row){
            $rowData = array();
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(FALSE);
            
            foreach($cellIterator as $cell){
                $rowData[] = $cell->getValue();
            }
            
            // 将数据插入数据库
            $name = $rowData[0];
            $time = $rowData[1];
            
            // 使用SQL语句将数据插入数据库
            // ...
        }
    }
}
?>
Copy after login

In the above code, we obtain the employee name and attendance time through the array index, and use SQL statements to insert the data into the database.

Summary:
By using PHP language, we can easily implement the import function of employee attendance data. This article starts with determining the data format and introduces in detail how to use HTML and PHP languages ​​to implement file upload and data processing functions. At the same time, code examples are provided to help readers understand better. I hope that through the introduction of this article, readers can successfully implement the employee attendance data import function.

The above is the detailed content of How to implement the employee attendance data import function through PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template