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

如何在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条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊