打开csv文件的问题

时间:2013-03-31 23:54:25

标签: php file-upload csv

嘿伙计我有以下内容:

session_start();
$seterror = 0;

function returnBack(){

    header("Location:../memberinterface.php?getmsg=15");
    exit;

}
function returnBackfile(){

    header("Location:../memberinterface.php?getmsg=16");
    exit;
}
if (!empty($_FILES['csvfile']['error'] ) )
    {

        $seterror = 1;
        returnBack();

    } 
    if (empty($_POST['firstname'])){

        $seterror =1;
        returnBack();
    }
    if (empty($_POST['lastname'])){
        $seterrro =1;

        returnBack();
    }
    if (empty($_POST['email'])){
        $seterror =1;

        returnBack();
    }
    if ($seterror !== 1){

    $file = $_FILES['csvfile']['tmp_name']; 

    $handle = fopen($file , "r");

    $fileop = fgetcsv($handle,0,"\t");
    echo '<pre>'; var_dump($fileop); var_dump($_POST); echo '</pre>';
    exit;
    $fileop=array_map("strtoupper",array_map("trim",$fileop));

    $firstname_index = array_search(strtoupper($_POST["firstname"]),$fileop);
    if ($firstname_index===false){
        returnBack();
    }
    $lastname_index = array_search(strtoupper($_POST["lastname"]),$fileop);
    if ($lastname_index===false){

        returnBack();
    }
    $email_index = array_search(strtoupper($_POST["email"]),$fileop);
    if ($email_index===false){

        returnBack();
    }
        //open file and store values
        $j = 0;
        while (($fileop=fgetcsv($handle,0,"\t")) !== false)
        {

            $fileop=array_map("trim",$fileop);
            $firstname[$j] = $fileop[$firstname_index];
            if (empty($firstname[$j])){ 

            returnBackfile();

            }

            $lastname[$j] = $fileop[$lastname_index];
                if (empty($lastname[$j])){

            returnBackfile();

            }
            $email[$j] = $fileop[$email_index];
                if (empty($email[$j])){

            returnBackfile();


            }
            $j++;
        }
        //create sessions, send back to memberinterface.php and open table to display the results to delete
        $_SESSION['CSVRfirstnames'] = $firstname;
        $_SESSION['CSVRlastnames'] = $lastname;
        $_SESSION['CSVRemails'] = $email;

        header("Location:../memberinterface.php?RemoveCSV=1");
    }

我回应列的部分:echo '<pre>'; var_dump($fileop); var_dump($_POST); echo '</pre>'; - 发生了一件奇怪的事情。

对于某些CSV文件,我得到了outprinted:

array(4) {
  [0]=>
  string(726) "PK!|l˜l [Content_Types].xml ¢( Ì”]KÃ0†ïÿCÉ­4Ù&ˆÈº]øq©çˆÍé–&!'›Û¿÷4û@¤nzÓÐæœ÷}’4ïp¼jL¶„€ÚÙ‚õye`K§´ìmú”ß²£´Jg¡`k@6]^§k˜Q·Å‚Õ1ú;!°¬¡‘ÈK3•ŒôfÂËr.g ½Þ(`c[
6>@%&f+ú¼!"
  [1]=>
  string(71) "`e÷›ÂÖ«`Ò{£K‰T,­úæ’o8u¦¬µÇ+Â`¢Ó¡ùÙ`Û÷B[´‚l"C|–
aˆ•.Ìß›óÃ"”®ªt"
  [2]=>
  string(951) "v€£ Ö±1<¼‘Úî¸ø§bi蟤]_>‘cðO8®ÿˆ#Òÿ"=$IæÈ`\À3¯v#z̹–Ôk”gøª}ˆƒîÑ$8”(Nß…]d´Ý¹'!QÃ>4º.ßÞ‘ÒètÃo·Ú¼S :¼EÊ×Ñ'ÿÿPK!µU0#õL_rels/.rels ¢( Œ’ÏNÃ0ÆïH¼CäûênH¡¥»LH»!TÀ$îµ£$@÷ö„‚JcÛÑöçÏ?[ÞîæiTb/Nú(A±3b{×jx­ŸV b"giÇŽaWÝÞl_x¤”›b×û¨²‹‹º”ü#b4Oñìr¥‘0QÊahÑ“¨eÜ”å=†¿P-<ÕÁj{ª>ú<ù²·4Mox/æ}b—NŒ@ž;ËvåCf©ÏÛ¨šBËIƒóœÓÉû"cž&Ú\Oôÿ¶8q"K‰ÐHàó<ߊs@ëë.Ÿh©ø½Î<⧄áMdøaÁÅT_ÿÿPK!Þ"
  [3]=>
  string(530) "(Ôxl/_rels/workbook.xml.rels ¢( ¼“ÏjÃ0ÆƒÑ}q’ne”:½ŒA¯[÷&QâÐÄ6–ö'o?“Cº@É.¡ƒ$ü}?Чýá§ïÄjU%)´¥«ZÛ(ø8½><ƒ Ö¶Ò³¨`@‚Cq·ÃNsüD¦õ$¢Š%†Ù襤Ò`¯)qmœÔ.ôšcéuyÖ
Ê
  string(9) "Firstname"
  ["lastname"]=>
  string(8) "lastname"
  ["email"]=>
  string(5) "Email"
}

和其他csv文件我得到很好的组织:

array(3) {
  [0]=>
  string(9) "firstname"
  [1]=>
  string(8) "lastname"
  [2]=>
  string(5) "email"
  [3]=>
}
array(3) {
  ["firstname"]=>
  string(9) "Firstname"
  ["lastname"]=>
  string(8) "lastname"
  ["email"]=>
  string(5) "email"
}

现在我不知道为什么我会为某些人获得奇怪的符号,我不希望那些如果你能帮助我那么好!

大卫

3 个答案:

答案 0 :(得分:2)

确保您的.CSV文件在Excel中保存为“逗号分隔”或“制表符分隔”。干杯!

答案 1 :(得分:0)

你可以在字符串上尝试utf8_encode()吗?

答案 2 :(得分:0)

您的某些“CSV”文件不是CSV文件。您正在打印的这种ackward结构可能是.xlsx文件或许多其他Microsoft Office 2007格式的内容。

如果我创建一个空白.docx.xlsx文件并在十六进制编辑器中看到,我会看到如下内容:

00000000h: 50 4B 03 04 14 00 06 00 08 00 00 00 21 00 DD FC ; PK..........!.Ýü
00000010h: 95 37 66 01 00 00 20 05 00 00 13 00 08 02 5B 43 ; •7f... .......[C
00000020h: 6F 6E 74 65 6E 74 5F 54 79 70 65 73 5D 2E 78 6D ; ontent_Types].xm
00000030h: 6C 20 A2 04 02 28 A0 00 02 00 00 00 00 00 00 00 ; l ¢..( .........
00000040h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................
00000050h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................
00000060h: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ; ................

很熟悉,不是吗?

这些文件实际上是.zip个文件,其中包含.xml文件树和属于您文档的其他资源。