php 匯入 csv

來源:互聯網
上載者:User

import.html

 

<form enctype="multipart/form-data" action="import.php" method="POST"><br /> 匯入模板<br /> <label for="檔案選擇">檔案選擇:</label><input name="csv_goods" type="file" /><br /> <input type="submit" value="匯入" name="import" /><br /></form>

 

import.php

 

<?php<br />if (isset($_POST['import'])){<br /> $file = $_FILES['csv_goods'];<br /> $file_type = substr(strstr($file['name'],'.'),1);<br /> // 檢查檔案格式<br /> if ($file_type != 'csv'){<br /> echo '檔案格式不對,請重新上傳!';<br /> exit;<br /> }<br /> $handle = fopen($file['tmp_name'],"r");<br /> $file_encoding = mb_detect_encoding($handle);<br /> // 檢查檔案編碼<br /> if ($file_encoding != 'ASCII'){<br /> echo '檔案編碼錯誤,請重新上傳!';<br /> exit;<br /> }<br /> $row = 0;<br /> while ($data = fgetcsv($handle,1000,',')){<br />//可以知道總共有多少行<br /> //echo "<font color=red>$row</font>";<br /> $row++;<br /> if ($row == 1)<br /> continue;<br /> $num = count($data);<br /> // 這裡會依次輸出每行當中每個儲存格的資料<br /> for ($i=0; $i<$num; $i++){<br /> echo $data[$i]."<br>";<br /> // 在這裡對資料進行處理<br /> }<br /> }<br /> fclose($handle);<br />}<br />?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.