PHP ประมวลผลไฟล์ zip จาก Google Form

โดย SONGCHAI SAETERN
ดาวน์โหลดข้อมูลจาก Google Form เอามาอัพโหลดประมวลผลใน PHP



ตัวอย่างโค้ด
<?php


if( isset($_FILES) && isset($_FILES["FileUpload1"]["name"]) && $_FILES["FileUpload1"]["name"] != '' ){
    $zip = zip_open($_FILES["FileUpload1"]["tmp_name"]);

    if ($zip)
      {
        while ($zip_entry = zip_read($zip))
        {
            echo "<p>";
            echo "<b>ชื่อไฟล์ : </b> <span class='text-primary'>" . zip_entry_name($zip_entry) . "</span><br />";

            if (zip_entry_open($zip, $zip_entry))
            {
                echo "File Contents:<br/>";
             
                $contents = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                $data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $contents));
                echo '<pre>';
                print_r($data);
                echo '</pre>';
                echo '<hr/>';
             
                zip_entry_close($zip_entry);
            }
           
            echo "</p>";
        }

    zip_close($zip);
    }
}

?>

<form enctype="multipart/form-data" action="index.php?page=gfmis&m=satisfaction_process" method="post">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h2 class="panel-title">
                <b>อัพโหลดข้อมูลแบบฟอร์มสำรวจความพึงพอใจ</b>
            </h2>
        </div>
        <div class="panel-body">
            <table cellspacing="0" cellpadding="0" border="0">
                <tbody>
                    <tr>
                        <td width="20">&nbsp;</td>
                        <td class="title" style="text-align:left">เลือกไฟล์ Excel</td>
                    </tr>
                    <tr>
                        <td></td>
                        <td class="title_content">
                            <input type="file" id="FileUpload1" name="FileUpload1">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" class="title_content">
                            <br/>
                            <input type="submit" id="Button1" value="ตรวจสอบข้อมูล" name="Button1">
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</form>


แหล่งอ้างอิง

PHP zip_entry_read() Function
https://www.w3schools.com/php/func_zip_entry_read.asp

Import CSV zipped file date to mysql
https://www.webmasterworld.com/php/4078430.htm

Converting CSV to array
https://stackoverflow.com/questions/7502370/converting-csv-to-array