NO 1. upload file

Test indotara persada
mail@pastecode.io avatar
unknown
php
2 months ago
2.4 kB
3
Indexable
Never
<!DOCTYPE html>
<html>
<head>
    <title>Upload File CSV</title>
</head>
<body>
    <h2>Upload File CSV</h2>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload File" name="submit">
    </form>

    <?php
    // Check if form is submitted
    if(isset($_POST["submit"])) {
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

        // Check if file is a CSV file
        if($fileType != "csv") {
            echo "Sorry, only CSV files are allowed.";
            $uploadOk = 0;
        }

        // Check if file was uploaded successfully
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
        } else {
            // Upload file to server
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

                // Read CSV file and display data in a table
                echo "<h3>Data from uploaded CSV file:</h3>";
                echo "<table border='1'>
                        <tr>
                            <th>Header XLXS</th>
                            <th>No</th>
                            <th>Nama</th>
                            <th>Tanggal (1-31)</th>
                            <th>Total Kehadiran</th>
                        </tr>";

                $file = fopen($target_file, "r");
                $row_count = 0;
                while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
                    if($row_count > 0) { // Skip header row
                        echo "<tr>";
                        foreach ($data as $value) {
                            echo "<td>".$value."</td>";
                        }
                        echo "</tr>";
                    }
                    $row_count++;
                }
                fclose($file);

                echo "</table>";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    }
    ?>
</body>
</html>
Leave a Comment