PHP 注意:无法上传未定义的偏移量数组文件


PHP Notice: Undefined offset array file can't upload

我已经打开了与我的帖子相关的所有问题,但我仍然感到困惑。我尝试使用 php 导入我的 csv 文件。

我尝试本教程。但是我得到了这个错误:

PHP Notice:  Undefined offset: 1
PHP Notice:  Undefined offset: 2
PHP Notice:  Undefined offset: 3
PHP Notice:  Undefined index: csv

我已经修改了我的数据库:

if ($_FILES["csv"]["size"] > 0) { 
    //get the csv file 
    $file = $_FILES["csv"]["tmp_name"]; 
    $handle = fopen($file,"r");
    //loop through the csv file and insert into database
//    do { 
        while ($data = fgetcsv($handle,1000,",","'")){
        print_r($data);
        if ($data[0]) { 
            mysql_query("INSERT INTO prod_schedule (RecNo,      Model_Name,     Lot_Number,     Start_Serial,
                                Qty,    Lot_Qty, Line,   Line_Name,      Start_Date,     End_Date,       Due_Date
                        ) VALUES 
                ( 
                    '".addslashes($data[0])."', 
                    '".addslashes($data[1])."', 
                    '".addslashes($data[2])."',
                    '".addslashes($data[3])."', 
                    '".addslashes($data[4])."',
                    '".addslashes($data[5])."',
                    '".addslashes($data[6])."', 
                    '".addslashes($data[7])."', 
                    '".addslashes($data[8])."',
                    '".addslashes($data[9])."' 
                ) 
            "); 
        } 
    } 
//      while ($data = fgetcsv($handle,1000,",","'"));
    // 
    //redirect 
    header('Location: upload.php?success=1'); die; 
}

print_r($data) 结果为:

Array ( [0] => RecNo Model_Name Lot_Number Start_Serial Qty Lot_Qty Line Line_Name Start_Date End_Date Due_Date ) Array ( [0] => 1 KD-R746BTU9D 011A 421 30 80 N0001 MA 01 3/3/2014 3/3/2014 12/31/2999 ) Array ( [0] => 2 KW-XG56T2UD 057A 25081 79 440 N0001 MA 01 3/3/2014 3/3/2014 12/31/2999 ) Array ( [0] => 3 KD-R646U9D 012B 561 1 60 N0001 MA 01 3/3/2014 3/3/2014 12/31/2999 )

编辑

改变while ($data = fgetcsv($handle,1000,",","'")){变得while ($data = fgetcsv($handle,1000,"'t","'")){.然后print_r($data):

array(1) { ["csv"]=> array(5) { ["name"]=> string(17) "prod_schedule.csv" ["type"]=> string(8) "text/csv" ["tmp_name"]=> string(14) "/tmp/phpb1ji8u" ["error"]=> int(0) ["size"]=> int(1943264) } }
Array ( [0] => RecNo [1] => Model_Name [2] => Lot_Number [3] => Start_Serial [4] => Qty [5] => Lot_Qty [6] => Line [7] => Line_Name [8] => Start_Date [9] => End_Date [10] => Due_Date ) 
Array ( [0] => 1 [1] => KD-R746BTU9D [2] => 011A [3] => 421 [4] => 30 [5] => 80 [6] => N0001 [7] => MA 01 [8] => 3/3/2014 [9] => 3/3/2014 [10] => 12/31/2999 ) 
Array ( [0] => 2 [1] => KW-XG56T2UD [2] => 057A [3] => 25081 [4] => 79 [5] => 440 [6] => N0001 [7] => MA 01 [8] => 3/3/2014 [9] => 3/3/2014 [10] => 12/31/2999 ) 
Array ( [0] => 3 [1] => KD-R646U9D [2] => 012B [3] => 561 [4] => 1 [5] => 60 [6] => N0001 [7] => MA 01 [8] => 3/3/2014 [9] => 3/3/2014 [10] => 12/31/2999 )

文件仍未上传,显示:PHP Notice: Undefined index: csv。然后像这样输入var_dump

var_dump($_FILES);
if ($_FILES["csv"]["size"] > 0) {

我得到了array(0) { }.

只是浏览一下,我想

 if (!empty($_GET[success])) 

应该是

 if (!empty($_GET['success'])) 

我认为问题是你如何放置你的 do while。 使用 do while 语句时,它将首先运行 do 中的代码,然后检查 while 语句。

你应该做什么,而不是

 do {
    if ($data[0]) {
        mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES
            (
                '".addslashes($data[0])."',
                '".addslashes($data[1])."',
                '".addslashes($data[2])."'
            )
        ");
    }
} while ($data = fgetcsv($handle,1000,",","'")); 

 while($data = fgetcsv($handle,1000,",","'")){
      if ($data[0]) {
        mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES
            (
                '".addslashes($data[0])."',
                '".addslashes($data[1])."',
                '".addslashes($data[2])."'
            )
        ");
    }

 }

基本上是一样的,它只是先执行while()中的代码,然后再执行其中的任何代码。

希望这有帮助。

通过您提供的 print_r($data) 的输出,我可以看到字段看起来是制表符分隔的。

您的代码在这里:

while ($data = fgetcsv($handle,1000,",","'")){

正在使用 fgetcsv 函数在字段之间查找标准逗号分隔符 - 这是常见的做法,因为 CSV 代表逗号分隔值。

若要使脚本读取使用制表符作为分隔符中的值,请尝试将上述行调整为:

while ($data = fgetcsv($handle,1000,"'t","'")){

希望这对:)有所帮助