dongqie2028 2015-01-24 11:38
浏览 47
已采纳

无法找到文件错误PHP

I am an extreme noob at this so please bear with me. I have this small project where I am trying to upload a csv file and insert it into MySQL.

I have read all the similar posts here and tried out the things that I understood but I am still getting errors :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 ',,,\\
'
        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>

I am getting the "Can't find file 'Book1.csv'" with this code. Please help!

EDIT: Finally got it to work. Here's the working code:

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

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





//$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.
";
} 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条回答 默认 最新

  • dousha7645 2015-01-24 12:43
    关注

    it would be so much better if have posted an image related to your error!And as php it self has expired the mysql functions it's better to use either PDO or mysqli! i have corrected some part of the codes! please check and let me know if it has helped or not.

                <?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 ',,,\\
    '
                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>
    

    the thing i have done is i have uploaded file and save it to the parent directory! and then read the file and when the import thing has finished i have deleted the file!

    hope it works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)