How to apply multiple image upload

Asked

Viewed 1,293 times

0

Hello I have this code that generates in the case of multiple adders with everything I’m not sure how to make an image upload code to upload all the images that are placed in FILE of the field of filing cabinet[ ] someone could help me supplement the code .

In case I need the code to upload to the medium folder, apply resizing, logo tag and Upe all images and add the values of the image in arquivo='".$archiveImage[$values]."' also since thank you who can help me.

Below is the code I did with everything without the image upload code. I’m still starting my course in PHP and there’s a lot I don’t know how to do.

I only managed to do only in case the repeat code using the for and add direct values to 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>

multiply.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>';
    }
    } ?>

1 answer

3

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

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

    The original file name on the client’s 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 submitted 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 really don’t see why 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: http://php.net/manual/en/faq.html.php#Faq.html.arrays

  • I already solved the problem of posting that was taken off the air -__- I thank Rafael Withoeft for the help provided in chat http://pastebin.com/xaF1PcKy

  • @Rodrigo you must refer to another question that you asked, that was closed, it did not come off the air. I must tell you that the questions here are simply not closed by wickedness, has grounds and is in line with community standards for good organization and other criteria. Don’t take this the wrong way I’ve had two questions of my own, I’ve just read the "Help" to better understand how to formulate a good question and refit (or edit) them to stay within the "scope".

Browser other questions tagged

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