upload multiple images with jqueryform and ajax

Asked

Viewed 115 times

1

Good afternoon, I’m trying to upload multiple images using jquery-form and ajax not to give Reload but is not passing to page via ajax by onchange and is not writing to the bank or uploading. follows code:

index.php
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" src="http://malsup.github.io/jquery.form.js"></script>
<form id="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-photos.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();
     }
  }
}
?>

1 answer

1


The function pathinfo returns the extension without the ., in its variable $allowedExts you specify the extensions with the ., so he’s not passing his test in_array($ext, $allowedExts), remove the vector points and should work

Browser other questions tagged

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