multiple image upload with jqueryform and ajax

1

Good afternoon, I'm trying to upload multiple images using jquery-form and ajax to not reload but is not going to the page via ajax by onchange and is not recording to the bank nor uploading. follow code:

index.php
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script><scripttype="text/javascript" src="http://malsup.github.io/jquery.form.js"></script><formid="formImage" enctype="multipart/form-data">
    <input type="text" name="id" value="<?php echo $resultado['id']; ?>" style="display:none;">
    <input type="file" id="files" style="float:left; width:700px; margin-top:0px;" name="fileUpload[]" onchange="saveImages()" multiple />
</form>

<script>
function saveImages()
  {
     $('#formImage').ajaxSubmit({
        url  : 'upload-fotos.php',
        type : 'POST'
     });
  }
</script>

upload-fotos.php

<?php
if(isset($_FILES['fileUpload']))
{
  //require("../config/wideimage-11.02.19-full/lib/WideImage.php");
  require("../config/conexao.php");
  $id = $_POST['id'];

  date_default_timezone_set('America/Sao_Paulo');

  $name     = $_FILES['fileUpload']['name'];
  $tmp_name = $_FILES['fileUpload']['tmp_name'];

  $allowedExts = array(".jpeg", ".jpg", ".png");

  $dir = '../uploads/servicos/';

  for($i = 0; $i < count($tmp_name); $i++)
  {
        $ext = pathinfo($name[$i], PATHINFO_EXTENSION);
        $ext = strtolower($ext);


     if(in_array($ext, $allowedExts))
     {
         $datetime = date("Y.m.d-H.i.s");
         $newtime = preg_replace('/[ :-]+/' , '' , $timedate);
        $new_name = $newtime ."-". $i . $ext;

        if(move_uploaded_file($new_name, $dir));
        //$image = WideImage::load($tmp_name[$i]);
       // $image->saveToFile($dir.$new_name);
        $sql = "INSERT INTO fotos_servicos(id_servico, img) VALUES (:idsv, :img)";
        $insert = $conn->prepare($sql);
        $insert->bindParam(':idsv', $id);
        $insert->bindParam(':img', $new_name);
        $conn->beginTransaction();
        $insert->execute();
        $conn->commit();
     }
  }
}
?>
    
asked by anonymous 28.11.2018 / 17:32

1 answer

1

The pathinfo function returns the extension without . , in its $allowedExts variable you specifies the extensions with the . , so it is not passing in its test in_array($ext, $allowedExts) , remove the points from the vector and it should work

    
28.11.2018 / 19:23