Upload multiple files in PHP

Asked

Viewed 889 times

6

I am developing an application in PHP where NF-e will be imported into the format XML. The problem is that the client will import multiple files at the same time, and I will have to take the data of each file and go inserting some information in the database. In this case, I evaluated the uploadify that in turn, there is an HTML5 version that is paid for, and another free version that renders on the page in swf form.

Is there any other multi-file upload library in PHP that could solve this kind of problem?

1 answer

2

Use the plugin jQuery Upload. In the plugin files there is an example of an image upload class. However, it is very complex.

To get the data that this plugin send, you can use equal is specified in the manual.

When a form is sent, the arrays $_FILES['userfile'], $_FILES['userfile']['name'], and $_FILES['userfile']['size'] are initialized.

Suppose you upload the files review.html and xwp.out using the following form:

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" value="Enviar" />
</form>

In this case in $_FILES['userfile']['name'][0] will take the value review.html and $_FILES['userfile']['name'][1] will have xwp.out.

The other file variables also have the same behavior

$_FILES['userfile']['size'][0]
$_FILES['userfile']['name'][0]
$_FILES['userfile']['tmp_name'][0]
$_FILES['userfile']['size'][0]
$_FILES['userfile']['type'][0]

Using this you can implement an upload class that suits you, and you can use the plugin I mentioned.

Another way that I believe is more feasible for you, is to send the zipped file.

You can read the compressed files using the ZipArchive

// Criando o objeto
$z = new ZipArchive();

// Abrindo o arquivo para leitura/escrita
$open = $z->open('teste.zip');
if ($open === true) {
    // Listando os nomes dos elementos
    for ($i = 0; $i < $z->numFiles; $i++) {
        // Obtendo o conteúdo pelo indice do arquivo $i
        $fileContent = $z->getFromIndex($i);
        // Aqui você faz o parser do XML e realiza sua manipulação
    }
    // Fechando o arquivo
    $z->close();
} else {
    echo 'Erro: '.$open;
}

Learn more about Ziparchive

Browser other questions tagged

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