读取CSV可以在不同的计算机上提供不同的输出

时间:2019-01-30 17:30:11

标签: php

因此,在我的平台上,我具有从CSV文件读取电子邮件,名字和姓氏,然后将它们保存在数据库中的功能。一切在我的计算机上都可以正常工作,但是在我的2个同事pc中,该列表返回0个元素,并且包含37个元素,但是在我的2台计算机上的多个浏览器中运行它,效果很好。我们都使用相同的列表进行测试。

<form action="" method="POST" enctype="multipart/form-data">
                               <label>Nome da lista:</label>
                               <input class="form-control" typr="text" name="nome" placeholder="Nome" /></br>
                               <label>Lista (Ficheiro CSV):</label>
                               <input type="file" name="file" id="file">

                                 <button type="submit" id="enviar" name="submit" style="max-width:100px; background: #ed1a3d; margin-top:10px;" class="btn btn-primary btn-lg btn-block btn-icon-split">
                                  Criar 
                                 </button>
                           </form>

<?php
global $user;
if(isset($_POST['submit'])) {

    if ($_FILES['file']['tmp_name']) {

        $nome = $_POST['nome'];
        $file = $_FILES['file']['tmp_name'];

        $user->criarLista($nome, $file);
    } else {
        $user->mensagem(1, "Não existe nenhum ficheiro");
    }

}
?>         

这是criarLista函数(这意味着创建列表):

public function criarLista($nome,$file){

        $user = $this->user;
        $get = $this->connect->query("SELECT * FROM users WHERE email = '$user'");
        $fetch = $get->fetch_array(MYSQLI_ASSOC);
        $user_id = $fetch['id'];

        if($insert = $this->connect->prepare("INSERT INTO lista(user_id,nome) VALUES(?,?)")){
            $insert->bind_param("is", $user_id,$nome);
            $insert->execute();
            $list_id = $insert->insert_id;

            $file = preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $file);
            $file = strip_tags($file);
            $lines = file($file);
            $emails = array();
            $fnames = array();
            $lnames = array();
            $linha = 0;
            foreach($lines as $line) {

                if($linha == 0){

                }else{

                    if (strpos($line, ',') !== false) {

                        $arr = explode(",", $line);
                        // Email \ FNAME | LAST

                        $emailx = trim($arr[0]);
                        $emailx = trim(preg_replace("/[\\n\\r]+/", "", $emailx));
                        array_push($emails,$emailx); 


                        if(isset($arr[1])){
                           $fname = trim($arr[1]);
                           $fname = str_replace('"','',$fname);
                           array_push($fnames,$fname); 
                        }

                        if(isset($arr[2])){
                           $lname = trim($arr[2]);
                           array_push($lnames,$lname); 
                        }

                    }else{
                       array_push($emails,trim($line)); 
                    }  

                }   

                $linha++;

            }

            array_map('trim', $emails);
            array_map('trim', $fnames);
            array_map('trim', $lnames);

            $emails = implode(",",$emails);
            $fnames = implode(",",$fnames);
            $lnames = implode(",",$lnames);

            if($insert_list = $this->connect->prepare("INSERT INTO listas(lista_id,email,primeiro_nome,ultimo_nome) VALUES(?,?,?,?)")){
                $insert_list->bind_param("isss", $list_id,$emails,$fnames,$lnames);
                $insert_list->execute();

                $this->mensagem(2,"Lista adicionada com sucesso");

            }else{
                echo 
                '
                <div class="alert alert-danger">
                    Erro: '.$this->connect->error.'
                </div>
                ';
            }


        }else{

            echo 
            '
            <div class="alert alert-danger">
                Erro: '.$this->connect->error.'
            </div>
            ';

        }    

    }

在某些计算机之间是否有可能导致这种不兼容性的东西?

0 个答案:

没有答案