Error importing multiple XML files - Codeigniter PHP

Asked

Viewed 39 times

0

I’m trying to import files xml into the database. In this example, I would have 2 arches xml to import. Note: XML is a road transport CTS. I tried to do it this way:

$i = 1;
$TotalArquivos = count($_FILES['arquivo']);

if($TotalArquivos==$i)
{
    echo $TotalArquivos." <<< TOTAL >>> "; 
} else 
{
    foreach($_FILES['arquivo'] as $file)
    {
        if($_FILES['arquivo']['tmp_name'][$i]==false) exit; //---> linha 39 do arquivo importarCET.php
        $xml[$i] = simplexml_load_file($_FILES['arquivo']['tmp_name'][$i]); 
        print_r(json_encode($xml[$i]))."<Br>";    
        $i++;
    }               

}

That way, my idea was within the $i, count how many you have and stop altogether. But he keeps searching... and generates the following error:

A PHP Error was encountered Severity: Notice Message: Undefined offset: 2 Filename: Libraries/importaCTE.php Line Number: 39

Where am I going wrong?

  • 1

    the $i must begin from 0 because it’s a array produced by PHP then starts with 0, whether it has two positions and 0 and 1 the positions of the array

  • 1

    There is also problem in the code, you make a foreach and no need to position anything already has the value in the other variavel

  • Right. I tried N forms, I used $i=0 as well. I tried using $file but was unsuccessful. :-(

  • I tried $file[$i] too

  • 1

    Saw the code?..

1 answer

1


A minimum example of how to get information from a field <input type="file"/> with PHP:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div>
  <form method="post" enctype="multipart/form-data">
    <input type="file" name="arquivo[]" multiple="multiple"/>
    <button>Enviar</button>
  </form>
  </div>
  <div>
  <?php
    if (empty($_FILES['arquivo']) === false){
      for($i = 0; $i < count($_FILES['arquivo']['name']); $i++){
        //$_FILES['arquivo']['name']
        //$_FILES['arquivo']['type']
        //$_FILES['arquivo']['tmp_name']
        //$_FILES['arquivo']['error']
        //$_FILES['arquivo']['size']
        simplexml_load_file($_FILES['arquivo']['tmp_name'][$i]);         
        //...
      }
    }
  ?>
  </div>
</body>
</html>
  • Good morning, in this case I am using Codeigniter. But it looks pretty interesting. I will test and return. Obg for help.

  • Good morning, thank you, it came right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.