douman6679 2011-04-08 10:00
浏览 48
已采纳

如何在php中上传和解析CSV文件

I want to upload a csv file with php. After the file is uploaded, I want to display the data of the CSV file. I would like an example how to accomplish this task.

  • 写回答

7条回答 默认 最新

  • dpr81047 2011-04-08 10:36
    关注

    Although you could easily find a tutorial how to handle file uploads with php, and there are functions (manual) to handle CSVs, I will post some code because just a few days ago I worked on a project, including a bit of code you could use...

    HTML:

    <table width="600">
    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
    
    <tr>
    <td width="20%">Select file</td>
    <td width="80%"><input type="file" name="file" id="file" /></td>
    </tr>
    
    <tr>
    <td>Submit</td>
    <td><input type="submit" name="submit" /></td>
    </tr>
    
    </form>
    </table>
    

    PHP:

    if ( isset($_POST["submit"]) ) {
    
       if ( isset($_FILES["file"])) {
    
                //if there was an error uploading the file
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    
            }
            else {
                     //Print file details
                 echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                 echo "Type: " . $_FILES["file"]["type"] . "<br />";
                 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                 echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
                     //if file already exists
                 if (file_exists("upload/" . $_FILES["file"]["name"])) {
                echo $_FILES["file"]["name"] . " already exists. ";
                 }
                 else {
                        //Store file in directory "upload" with the name of "uploaded_file.txt"
                $storagename = "uploaded_file.txt";
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $storagename);
                echo "Stored in: " . "upload/" . $_FILES["file"]["name"] . "<br />";
                }
            }
         } else {
                 echo "No file selected <br />";
         }
    }
    

    I know there must be an easier way to do this, but I read the CSV file and store the single cells of every record in an two dimensional array.

    if ( isset($storagename) && $file = fopen( "upload/" . $storagename , r ) ) {
    
        echo "File opened.<br />";
    
        $firstline = fgets ($file, 4096 );
            //Gets the number of fields, in CSV-files the names of the fields are mostly given in the first line
        $num = strlen($firstline) - strlen(str_replace(";", "", $firstline));
    
            //save the different fields of the firstline in an array called fields
        $fields = array();
        $fields = explode( ";", $firstline, ($num+1) );
    
        $line = array();
        $i = 0;
    
            //CSV: one line is one record and the cells/fields are seperated by ";"
            //so $dsatz is an two dimensional array saving the records like this: $dsatz[number of record][number of cell]
        while ( $line[$i] = fgets ($file, 4096) ) {
    
            $dsatz[$i] = array();
            $dsatz[$i] = explode( ";", $line[$i], ($num+1) );
    
            $i++;
        }
    
            echo "<table>";
            echo "<tr>";
        for ( $k = 0; $k != ($num+1); $k++ ) {
            echo "<td>" . $fields[$k] . "</td>";
        }
            echo "</tr>";
    
        foreach ($dsatz as $key => $number) {
                    //new table row for every record
            echo "<tr>";
            foreach ($number as $k => $content) {
                            //new table cell for every field of the record
                echo "<td>" . $content . "</td>";
            }
        }
    
        echo "</table>";
    }
    

    So I hope this will help, it is just a small snippet of code and I have not tested it, because I used it slightly different. The comments should explain everything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来