Applying multiple image uploads

0

Hello, I have this code that generates in the case of multiple additives with everything I'm not sure how to make an image upload code to upload all the images that are placed in the FILE field of < strong> file [] could anyone help me to complement the code.

In case I need the code to upload to the media folder, apply scaling, then mark and upe all the images and add the image values in arquivo = '". $ imageImage [$ values ]. "' I also thank you for being able to help me.

Below is the code that I did with everything without the image upload code. I'm still starting my PHP course and there's a lot I still do not know how to do.

I've just been able to do just the repeat code using for and add the values directly to the database .

index.php

<form id="form1" name="form1" method="post" action="multiplicar.php">
  Coloque aqui o numero de episodios que vc ira adicionar 
  <label>
    <input type="text" name="numeros" id="numeros" style="width:20px;" />

  </label>
  <label>
    <input type="submit" name="button" id="Gerar" value="Gerar" />
  </label>
</form>

multiplicar.php

<form id="form1" enctype="multipart/form-data" name="form1" method="post" action="salvar.php">
    <?php
    $valor = $_POST["numeros"];
    for ($repeticao = 1; $repeticao <= $valor; $repeticao++) { ?>
      <label>
       Nome  <input name="categorias[]" type="text" id="numero" value="" size="10" />  Tipos  <input name="tipos[]" type="text" id="tipos" value="" size="10" /></br>
       Imagem <input name="arquivo[]" type="file" id="arquivo[]" style="border:0px; width:249px;box-shadow: 0px 0px 2px #5B5B5B;border-radius:0px; font-family:'Arial'; font-size:14px; padding-left:0px; padding-right:0px;"/> 
      </label>
    <? }  ?>
    <input type="submit" name="button" id="button" value="Enviar" />
    </form>

save.php

<?php   
    $categoria = $_POST["categorias"];   
    $tipo = $_POST["tipos"];   
    $arquivoImagem = $_POST["arquivo"];   
    $numeracao = 1;
    foreach($categoria as $valores => $maiorIgual) {


    // Faz a inserção dos dados na MYSQL
    $sql = "INSERT INTO 'medias' SET 'cat'='".$categoria[$valores]."', 'tipos'='".$tipo[$valores]."', 'arquivo'='".$arquivoImagem[$valores]."'";
    $consulta = mysql_query($sql);
    if($consulta) {
    echo'<center>';
    echo " Item ".$numeracao++." Cadastrado com Sucesso<br/>";
    }else{
    echo " Item ".$numeracao++." Erro ao cadastrar<br/>";
    echo'</center>';
    }
    } ?>
    
asked by anonymous 12.04.2015 / 14:46

1 answer

3

Do not use $arquivoImagem = $_POST["arquivo"]; use $arquivoImagem = $_FILES["arquivo"];

  • $_FILES['userfile']['name']

    The original file name on the client machine.

  • $_FILES['userfile']['type']

    The mime type of the file, if the browser provides this information. An example could be image/gif . The mime type however is not checked by PHP so do not consider that this value will be granted.

  • $_FILES['userfile']['size']

    The size, in bytes, of the uploaded file.

  • $_FILES['userfile']['tmp_name']

    The temporary name with which the uploaded file was stored on the server.

  • $_FILES['userfile']['error']

    The error code associated with this file upload.

  

Note: I do not really see a reason to use foreach if the array is not associative.

Example:

foreach ($categoria as $valores => $maiorIgual) {
    $name = $_FILES['file'][$valores]['name'];
    $tmp_name = $_FILES['file'][$valores]['tmp_name'];

    $error = $_FILES['file'][$valores]['error'];
    if ($error !== UPLOAD_ERR_OK) {
        echo 'Erro ao fazer o upload de:', $name, ' / Erro: ', $error;
    } elseif (move_uploaded_file($tmp_name, $location . $name)) {
        $sql = "INSERT INTO 'medias' SET 'cat'='".$categoria[$valores]."', 'tipos'='".$tipo[$valores]."', 'arquivo'='".$arquivoImagem[$valores]."'";
        //Resto do seu código  
    }
}

I recommend that if you do not use associative arrays, use for normal instead of foreach

$categoria = $_POST["categorias"];   
$tipo = $_POST["tipos"];   
$arquivoImagem = $_POST["arquivo"];

$total = count($arquivoImagem);

if ($total !== count($tipo) || $total !== count($categoria)) {
    //Adicionei este if só como segurança com problemas no formulário
    echo 'Quantidade de itens é invalida';
} else {
    for ($i = 0; $i < $total; $i++) {
        $name = $_FILES['file'][$i]['name'];
        $tmp_name = $_FILES['file'][$i]['tmp_name'];

        $error = $_FILES['file'][$i]['error'];
        if ($error !== UPLOAD_ERR_OK) {
            echo 'Erro ao fazer o upload de:', $name, ' / Erro: ', $error;
        } elseif (move_uploaded_file($tmp_name, $location . $name)) {
            $sql = "INSERT INTO 'medias' SET 'cat'='".$categoria[$i]."', 'tipos'='".$tipo[$i]."', 'arquivo'='".$arquivoImagem[$i]."'";
            //Resto do seu código  
        }
    }
}

To better understand the use of HTML arrays: link

p>     
12.04.2015 / 15:46