5
I’m having a relationship problem between the caption and the photo while uploading.
I’m uploading multiple photos and each photo has a caption. The code goes something like this
html
<!-- form via post-->
<input type="file" id="fotos" name="fotos[]" multiple/><br>
<!-- galeria gerado dinamicamente conforme é selecionado as fotos -->
<div id="galeria"></div>
javascript
//contador de fotos, aqui está o problema.
var qtdFotos=0;
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
f.id=qtdFotos;
qtdFotos++;
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
var image;
image = '<div class="content-foto">';
image += '<img id="foto'+theFile.id+'" class="thumb" src="'+e.target.result+'" ></img>';
image += '<p>Descrição:</p>';
//estou usando o contador de fotos aqui para diferenciar os nomes dos input, mas não está funcionando da forma que quero.
image += '<input name="descricao_foto'+theFile.id+'" type="text"></input>';
image += '<p>Fotógrafo:</p>';
image += '<input name="fotografo_foto'+theFile.id+'" type="text"></input>';
image += '</div>';
$('#galeria').append(image);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('fotos').addEventListener('change', handleFileSelect, false);
php
$fotos = array();
for($i=0;$i<count($_FILES['fotos']['name']);$i++){
if($files["fotos"]["error"][$i] != 0)
continue;
$foto = new Foto();
//aqui a descrição não está correspondendo com a foto.
$foto->nome = $_FILES['fotos']['name'][$i];
$foto->descricao = $_POST['descricao_foto'.$i] ;
$foto->fotografo = $_POST['fotografo_foto'.$i] ;
$foto->tmp_name = $_FILES['fotos']['tmp_name'][$i];
$fotos[] = $foto;
}
salvarFotos($fotos);
The problem I’ve identified is in the photo counter, the for
that I do in the javascript
does not correspond with the for
that I do in the php
.Because the order of the photos that is passed via post is not the same that is created in javascript. Causing the Wrong Relationship Between Writing and Photographer With Wrong Photo
The code works yes, after testing the problem is another, when I select once and open the option to select again, the photos from the first selection are not sent. only the photos from the last selection are sent, then the relationship problem happens, so when you open the photo selection screen again the ones that were selected before are replaced.
– Skywalker