上载多个TXT文件,并验证每个文件上的列长

时间:2019-01-10 16:06:26

标签: php arrays laravel validation file-upload

我有一只蝙蝠将.txt文件导入数据库,这很麻烦,所以已经开始构建Web导入,我只完成了一次输入,但是需要导入多个

它是过程的路径

上传文件 验证文件ex的列长度:“如果房屋编号<> 6,则...” 如果所有列的长度都正确,则传递到第二个文件

它仅处理输入的第一个文件

函数impjucespupload(请求$ request)     {     $ qtdarquivos = count($ request-> file('upload-file')); //获取上传的文件总数

$upload = $request->file('upload-file');
foreach($upload as $file){
    $name = $file->getRealPath();
    $data[]=$name; //make array with the multiple files
}

$i=0;

for($a=1;$a< $qtdarquivos; $a++){ //validate the first file of the array upload
    $file = fopen($data[$a],'r');

    while (($line = fgets($file)) !== false){
        $data = explode(chr(9) , $line);
        $locks[$a][$i] = array(
            0 => $data[0],
            1 => $data[1],
            2 => $data[2],
            3 => $data[3],
            4 => str_replace("\n", "", $data[4])
        );
        $i++;
        }


        $tamanho = count($locks); //get total of lines 

        $d = 0;
        $e = 0;
        $erro = 0;
        for ($e = 0; $e < $tamanho; $e++) //iterate the columns of txt
            {
            if (strlen($locks[$a][$d][0]) <> 6)
                {
                $erro = 1;

                return back()->withErrors('Rolo Inválido na Linha ' . $d = $d + 1); // stop and return alert to view if found any error
                }
              else // 
                {
                $d++; //iterate the next line of the first file 
                }
            }
          $a++; //sum +1 to go for the validation of next file uploaded that be in on $data[]array

          $b++; //For open the second file
}

我只需要上传10个.txts的示例并验证列的长度,如果发现任何错误就中断,然后插入数据库

1 个答案:

答案 0 :(得分:0)

您正在$data循环中覆盖while变量。

while (($line = fgets($file)) !== false){
    $data = explode(chr(9) , $line);

更改第二行以使用其他变量名。