Multiple Upload images

1

I want to perform multiple uploads but the following message appears:

  

Notice: Undefined index: files in C: \ xampp \ htdocs \ fotolog \ adm \ upload_fotos.php on line 6

     

Warning: Invalid argument supplied for foreach () in C: \ xampp \ htdocs \ fotolog \ adm \ upload_fotos.php on line 6

Javascript:

$(document).ready(function(){

    $('#uploadForm').on('submit',function(e)){
        e.preventDefault();
        $.ajax({
            url:"upload_fotos.php"
            data: new FormData(this),
            contentType: false,
            processData: function(data)
            success: function(data){
                $("#galley").html(data);
                alert("imagem foi");

            }
        });
    });
});
<div class="container">  
    <form id="uploadForm" action="upload_fotos.php" method="post">  
        <div id="gallery"></div><div style="clear:both;"></div><br /><br />  
        <div class="col-md-4" align="right">  
            <label>Upload Multiple Image</label>  
        </div>  
        <div class="col-md-4">  
            <input name="files[]" type="file" multiple />  
        </div>  
        <div class="col-md-4">  
            <input type="submit" submitvalue="Submit" />  
        </div>  
        <div style="clear:both"></div>  
    </form>  
</div>
</form>

        

PHP:

 <?php  
 //upload.php  
 $output = '';  
 if(is_array($_FILES))   
 {  
      foreach ($_FILES['files']['name'] as $name => $value)  
      {  
        $file_name = explode(".", $_FILES['files']['name'][$name]);  
        $allowed_extension = array("jpg", "jpeg", "png", "gif"); 
           if(in_array($file_name[1], $allowed_ext))  
           {  
                $new_name = md5(rand()) . '.' . $file_name[1];  
                $sourcePath = $_FILES['files']['tmp_name'][$name];  
                $targetPath = "foto/".$new_name;  
                if(move_uploaded_file($sourcePath, $targetPath))  
                {  
                     $output .= '<img src="'.$targetPath.'" width="150px" height="180px" />';  
                }                 
           }            
      }  
      echo $output;  
 }  
 ?>  
    
asked by anonymous 16.04.2017 / 22:21

1 answer

2

There is so much wrong in your code that I do not even know how you got the error, in fact I think due to this series of errors instead of Ajax the form was being sent by normal request since it had errors typed in the JS he did not even process. See the problems:

  • It seems like you do not quite understand what Json is (object)

    $.ajax({                          <--- Falta definir o type: POST
        url:"upload_fotos.php"        <--- Falta virgula
        data: new FormData(this),
        contentType: false,
        processData: function(data)   <--- Função não terminada, na verdade isto deveria ser false e não uma função
        success: function(data){
            $("#galley").html(data);
            alert("imagem foi");
    
        }                             <--- Falta definir o error:function(), é fundamental
    });
    
  • You changed the name of the variables:

    $allowed_extension = array("jpg", "jpeg", "png", "gif"); 
    
    if(in_array($file_name[1], $allowed_ext))
    

    In one place you use $allowed_extension and in the other $allowed_ext .

  • Your problems are a series of flaws of your own with typing errors, and you probably have not attempted to read the jQuery documentation, I'll give you a hint Ctrl C and Ctrl + V in codes up works, but it will not do much good if you do not know the least of what you're doing. / p>

    Corrected code

    jQuery ( recommend ) to read link documentation:

    $(document).ready(function(){
        $('#uploadForm').on('submit', function(e) {
            e.preventDefault();
            $.ajax({
                type: "POST", //O type que faltava
                url: "upload_fotos.php",
                processData: false, //Aqui tem que ser false para pode enviar FormData
                contentType: false,
                data: new FormData(this),
                success: function(data){
                    $("#galley").html(data);
                    alert("imagem foi");
                },
                error: function(a, b, c) { //Isto ser para tratar erros de HTTP ou conexão
                    alert([a, b, c]);
                }
            });
        });
    });
    

    PHP may look like this:

    if(is_array($_FILES))
    {
        $output = '';
    
        foreach ($_FILES['files']['name'] as $name => $value)
        {
            $file_name = explode(".", $_FILES['files']['name'][$name]);
            $allowed_extension = array("jpg", "jpeg", "png", "gif");
    
            if(in_array($file_name[1], $allowed_extension))
            {
                $new_name = md5(rand()) . '.' . $file_name[1];
                $sourcePath = $_FILES['files']['tmp_name'][$name];
                $targetPath = "foto/".$new_name;
    
                if(move_uploaded_file($sourcePath, $targetPath))
                {
                    $output .= '<img src="'.$targetPath.'" width="150px" height="180px" />';
                }
            }
        }
        echo $output;
    }
    
        
    16.04.2017 / 23:51