Multiple files with HTML and PHP

Asked

Viewed 264 times

-2

I’m with an XML file reading system in progress, I chose the language that I do not have as much PHP ease, the fact and that I do not know how to take several files and allocate in a variable, I’m only taking the temporary address of the files and do the reading, all the process that has to be done and shown on the screen is already working but unified, I can only do reading only 1 file by you and need to read several files at a time.

Does anyone know how to read many XML ?

Entry code:

<form action="teste2.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="entXML" multiple="multiple"><br>
    <input type="submit" value="LER_ARQUIVO">
</form>

Treatment with PHP

<?php

//Instanciando o OBJ
 $xml = new DOMdocument();

 //Pegando o TMP do FORM
 $arquivo = $_FILES['entXML']['tmp_name'];

 //Carregando o XML
 $xml ->load($arquivo) or die("Erro ao carregar arquivo XML");



  //Entra dentro do NÓ pai do XML
  $arquivoXML = $xml->getElementsByTagName( "nfeProc" );



//Navegando no Nodes(Nós)
  foreach( $arquivoXML as $xml){

          //Pega Nome
  $nomes = $xml->getElementsByTagName( "xNome" );
  $nome = $nomes->item(0)->nodeValue;
        //Pega CNPJ
  $cnpjS = $xml->getElementsByTagName( "CNPJ" );
  $cnpj = $cnpjS->item(0)->nodeValue;

  //Mostra dados da Tabela EMIT
  echo "<b>Nome:</b> $nome"."&nbsp&nbsp&nbsp";
  echo "<b>CNPJ:</b> $cnpj";

  echo"<br>";
  echo"<br>";
  echo"<br>";


  echo"-------------------------------VALORES----------------------------------";
  echo"<br>";
  //-----------------------------------------------------------------

  //Entra dentro do NODE "ide"
foreach( $arquivoXML as $xml){
        //Pega Numero da NF
          $nfs = $xml->getElementsByTagName( "nNF" );
          $nf = $nfs->item(0)->nodeValue;
        //Pega Data de Emissão da NF
          $dataEMS = $xml->getElementsByTagName( "dhEmi" );
          $dataEmi = $dataEMS->item(0)->nodeValue;
        //Pega CFOP
          $cfopS = $xml->getElementsByTagName( "CFOP" );
          $cfop = $cfopS->item(0)->nodeValue;
        //Pega Valor do XML
          $vaPagS= $xml->getElementsByTagName( "vPag" );
          $vaPag = $vaPagS->item(0)->nodeValue;
        //Pega Status da Sefaz XML
          $motivoS= $xml->getElementsByTagName( "xMotivo" );
          $motivo = $motivoS->item(0)->nodeValue;

//Mostra Tabela na Tela
  echo "<table border=1;>";

      echo "<tr>";
          echo "<th>Nº NF</th>" ;
          echo "<th>Data Emissão</th>" ;
          echo "<th>CFOP</th>" ;
          echo "<th>Valor</th>" ;
          echo "<th>Status</th>" ;
      echo "</tr>";

      echo"<tr>";
          echo"<td>$nf</td>";
          echo"<td>$dataEmi</td>";
          echo"<td>$cfop</td>";
          echo"<td>$vaPag </td>";
          echo"<td>$motivo </td>";
      echo"</tr>";

  echo "</table>"; 
 }
}

RESULTADO DE SAIDA

  • I think what Voce really needs is to open multiple files as an https://www.sobolsoft.com/howtouse/combine-xml-files.htm. I hope I helped you!

  • No and that, I need to work on my code for it to read multiple XML, anyway Thanks for the help !

1 answer

0


If you need to grab multiple files, then you need to treat as an array.

Change the name of adding [] in the end, in this way:

<form action="teste2.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="entXML[]" multiple="multiple"><br>
    <input type="submit" value="LER_ARQUIVO">
</form>

From now on entXML is an array of files and can be recovered in this way:

 //Pegando o TMP do FORM
 $arquivos = $_FILES['entXML']['tmp_name'];

or for example:

 $arquivo1 = $_FILES['entXML']['tmp_name'][0];
 $arquivo2 = $_FILES['entXML']['tmp_name'][1];

In this link should be clearer.


You can try to make a foreach upon the //Carregando o XML, for each position of $_FILES['entXML']['tmp_name'];, for example:

foreach($arquivos as $arquivo) {
    //Carregando o XML
    $xml ->load($arquivo) or die("Erro ao carregar arquivo XML");
    ...
  • Okay, I got that part and I’ve already set up php.ini and now I can get up to 1000 addresses of XML files, but I can’t get inside all these XML files and get the Nodesvalue I need, I am manually picking up the values with foreach and picking up the addresses using the for loop but to read these various paths which loop ultilizo ? That’s what I’m not getting !

  • <?php //Instantiating OBJ $xml = new Domdocument(); $path = $_FILES['entXML']['tmp_name']; for( $i = 0; $i < Count($path);$i++)' $xml ->load($path[$i]) or die("Error loading XML file"); $arquivoXML = $xml->getelementsbytagname( "nfeProc" ); //NEED TO DO THIS FOREACH RUN IN //ALL XML FILES foreach( $arquivoXML as $xml){ $names = $xml->getelementsbytagname( "xNome" ); $name = $names->item(0)->nodeValue; echo "<b>Name:</b> $name"." &nbsp&nbsp&nbsp"; } }

  • I have edited my answer, you have given yourself to understand?

  • Good morning, in case then it would be 2 foreach a load pro that in this case it will load the XML in their respective positions and another to load the NODES data and that ? I’m trying to implement this code here

  • This, a foreach above to read every xml Voce uploaded

  • <?php $xml = new Domdocument(); $file = $_FILES['path']['tmp_name']; for( $i = 0; $i < Count($file);$i++){ foreach ($file as $file[$i]) { //Opening Obj for reading NODES(Nodes) $xml ->load($file[$i]) or die("Error loading XML file"); //Enters Table "Emit" $arquivoXML = $xml->getelementsbytagname( "nfeProc" ); //Picks Name $names = $xml->getelementsbytagname( "xNome" ); $name = $names->item(0)->nodeValue; echo "<b>Name:</b><b> $name </b>"." <br>"; } }

  • This way I mentioned above it works but duplicates the values, for example if I select two Xmls it shows 2x plus the fields I want to print on the screen

Show 2 more comments

Browser other questions tagged

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