Uploading multiple files in PHP

6

I'm developing an application in PHP where import of NF-e will be done in XML format. The problem is that the client will do this import of 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 uploadify which in turn, there is an HTML5 version that is paid for, and another free version that renders the page in swf form.

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

    
asked by anonymous 11.02.2014 / 17:44

1 answer

2

Use the jQuery Upload plugin. In the plugin files there is an example class for image upload. But it's very complex.

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

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

Let's suppose you upload review.html and xwp.out files 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 have 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 I think is more feasible for you is to send the zipped file.

You can read the compressed files using 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

    
12.02.2014 / 02:05