无法上传csv文件并获得错误C:\ xampp \ tmp \ php9F4F.tmp

时间:2018-04-06 07:31:36

标签: php mysql database csv xampp

我正在开发一个网站,要求我创建上传.csv文件的功能。 上传文件时,表示格式错误,并显示以下消息:

  

C:\ XAMPP \ TMP \ php9F4F.tmp

p / s:我已经将excel文件转换为.csv逗号分隔格式,并在上传时仍然出错。

我该怎么做才能解决错误,然后将csv文件上传到我的数据库?

stdreport.php://我在这里使用import.php中的相同代码将它与数据库连接

<?php
$SQLSELECT = "SELECT stdCard, stdName, stdProgram, stdCourseDesc, stdCampus 
                FROM student 
                ORDER BY stdID 
                LIMIT 0,20";

$result_set =  mysql_query($SQLSELECT, $conn);
while($row = mysql_fetch_array($result_set))
{
?>

import.php

<?php 
$conn=mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("dashboard",$conn) or die("could not connect database");

if(isset($_POST["Import"])){

    echo $filename=$_FILES["file"]["tmp_name"];

    if($_FILES["file"]["size"] > 0) {

        $file = fopen($filename, "r");
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {

            //It wiil insert a row to our subject table from our csv file`
            $sql = "INSERT into student 
                            (`stdID`, `stdCard`, `stdName`, 
                            `stdOfficialEmail`,`stdEmail`, 
                            `stdContNum`,`stdCourseDesc`, `stdCampus`, 
                            `accessDate`) 
                    values('$emapData[1]','$emapData[2]','$emapData[3]',
                            '$emapData[4]','$emapData[5]',
                            '$emapData[6]', '$emapData[7]','$emapData[8]',
                            '$emapData[9]')";

            //we are using mysql_query function. it returns a resource on true else False on error
            $result = mysql_query( $sql, $conn );
            if(! $result ) {
                echo "<script type=\"text/javascript\">
                        alert(\"Invalid File:Please Upload CSV File.\");
                        window.location = \"stdreport.php\"
                    </script>";
            }
        }
        fclose($file);
        //throws a message if data successfully imported to mysql database from excel file
        echo "<script type=\"text/javascript\">
                alert(\"CSV File has been successfully Imported.\");
                window.location = \"stdreport.php\"
            </script>";

             //close of connection
            mysql_close($conn); 
         }
    }    
?>       

1 个答案:

答案 0 :(得分:0)

当你将excel文件转换为CSV时,有两种类型的合成CSV UTF-8(逗号分隔),第二种是CSV(逗号分隔),使用我附加屏幕截图的第二个选项。

enter image description here