无法找到文件错误PHP

时间:2015-01-24 11:38:40

标签: php mysql csv file-handling

我是一个极端的菜鸟,所以请耐心等待。 我有这个小项目,我试图上传一个csv文件并将其插入MySQL

我在这里阅读了所有相似的帖子并尝试了我理解的内容,但我仍然遇到错误:D

    <?php

$databasehost = "localhost";
$databasename = "hutchreport";
$databasetable = "intervalreport";
$databaseusername ="root";
$databasepassword = "";

if(isset($_POST['SUBMIT']))
{

     $fname = $_FILES['csv_file']['name'];
     $chk_ext = explode(".",$fname);

     if(strtolower($chk_ext[1]) == "csv")
     {

         $filename = $_FILES['csv_file']['tmp_name'];

        $con = @mysql_connect($databasehost,$databaseusername,
                              $databasepassword) or die(mysql_error());

        @mysql_select_db($databasename) or die(mysql_error());

        $sql = "LOAD DATA LOCAL INFILE '$fname'
        INTO TABLE intervalreport
        FIELDS TERMINATED BY ','
        LINES TERMINATED BY ',,,\\r\\n'
        IGNORE 1 LINES (intervstartdate, intervstarttime, intervenddate,
                        intervendtime, loginname, loginnumber,
                        callsoffered, callsanswered, abandonedcalls,
                        waittime, staffedtime, auxtime, meeting, 
                        coaching, logintime, inboundtalktime, 
                        avginboundtalktime, inboundacwtime, 
                        avginboundacwtime, inboundhandlingtime, 
                        avginboundhandlingtime, heldcalls, 
                        inboundholdtime, avginboundholdtime, 
                        notreadytime, avgnotreadytime)";

        mysql_query($sql) or die(mysql_error());



         fclose($handle);
         echo "Successfully Imported";
     }
     else
     {
         echo "Invalid File";
     }    
}

?>

<form action='<?php echo $_SERVER["PHP_SELF"];?>' enctype="multipart/form-data" method='post'>
    <input type='file' name='csv_file' size='20'>
    <input type='submit' name='SUBMIT' value='SUBMIT'>
</form>
</body>
</html>

我使用此代码获取“无法找到文件'Book1.csv'”。请帮忙!

编辑:终于让它发挥作用了。这是工作代码:

<html>
<head>
<title>
test
</title>
</head>
<body>

<?php
$databasehost = "localhost"; 
$databasename = "hutchreport"; 
$databasetable = "intervalreport"; 
$databaseusername="root"; 
$databasepassword = ""; 
$fieldseparator = ","; 
$lineseparator = "\n";





//$csvfile = "csv/Book1.csv";

if(isset($_POST['SUBMIT']))
{
        $csvfile = $_FILES['csv_file']['tmp_name'];
        move_uploaded_file($csvfile, $csvfile);
        if(!file_exists($csvfile)) {
            die("File not found. Make sure you specified the correct path.");
        }

        try {
            $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", 
                $databaseusername, $databasepassword,
                array(
                    PDO::MYSQL_ATTR_LOCAL_INFILE => true,
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                )
            );
        } catch (PDOException $e) {
            die("database connection failed: ".$e->getMessage());
        }

        $affectedRows = $pdo->exec("
            LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
            FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
            LINES TERMINATED BY ".$pdo->quote($lineseparator)."IGNORE 1 LINES (intervstartdate, intervstarttime, intervenddate, intervendtime, loginname, loginnumber, callsoffered, callsanswered, abandonedcalls, waittime, staffedtime, auxtime, meeting, coaching, logintime, inboundtalktime, avginboundtalktime, inboundacwtime, avginboundacwtime, inboundhandlingtime, avginboundhandlingtime, heldcalls, inboundholdtime, avginboundholdtime, notreadytime, avgnotreadytime)");

        echo "Loaded a total of $affectedRows records from this csv file.\n";
} else { echo "invalid file";}

?>

<form action="" enctype="multipart/form-data" method='post'>
        <input type='file' name='csv_file' size='20'>
        <input type='submit' name='SUBMIT' value='SUBMIT'>
    </form>
    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

如果发布了与您的错误相关的图像会好得多!而且作为php,它自己已经过期了mysql函数,它最好使用PDO或mysqli! 我已经纠正了部分代码!如果有帮助,请检查并告诉我。

            <?php

    $databasehost = "localhost";
    $databasename = "hutchreport";
    $databasetable = "intervalreport";
    $databaseusername ="root";
    $databasepassword = "";

    if(isset($_POST['SUBMIT']))
    {

         $fname = $_FILES['csv_file']['name'];
         $chk_ext = explode(".",$fname);

         if(strtolower($chk_ext[1]) == "csv")
         {

             $filename = $_FILES['csv_file']['tmp_name'];

            $con = new mysqli($databasehost,$databaseusername,$databasepassword, databasename);
move_uploaded_file($filename, $filename);
            $sql = "LOAD DATA LOCAL INFILE {$filename}
            INTO TABLE intervalreport
            FIELDS TERMINATED BY ','
            LINES TERMINATED BY ',,,\\r\\n'
            IGNORE 1 LINES (intervstartdate, intervstarttime, intervenddate,
                            intervendtime, loginname, loginnumber,
                            callsoffered, callsanswered, abandonedcalls,
                            waittime, staffedtime, auxtime, meeting, 
                            coaching, logintime, inboundtalktime, 
                            avginboundtalktime, inboundacwtime, 
                            avginboundacwtime, inboundhandlingtime, 
                            avginboundhandlingtime, heldcalls, 
                            inboundholdtime, avginboundholdtime, 
                            notreadytime, avgnotreadytime)";

            $con->query($sql);



             fclose($handle);
unlink($filename);
             echo "Successfully Imported";
         }
         else
         {
             echo "Invalid File";
         }    
    }

    ?>

    <form action="" enctype="multipart/form-data" method='post'>
        <input type='file' name='csv_file' size='20'>
        <input type='submit' name='SUBMIT' value='SUBMIT'>
    </form>
    </body>
    </html>

我所做的是我上传了文件并将其保存到父目录!然后读取文件,当导入事件完成后我删除了文件!

希望它有效