Jump to content

Recommended Posts

I did this code in HTML

<label id="read" class="read" for="files">Choose:</label>
           <input type="file" accept="image/*|video/*" name="file" id="file" class="jump" multiple="true">
           <input type="button" id="btlo" class="btload" name="load" value="Load" onclick="Load();">

 

and call function Load() below

 

function Load(){
     var fileInput = document.getElementById("file");  
      var str = new FormData();
       for (i = 0; i < fileInput.files.length; i++) {
         str.append(fileInput.files.name, fileInput.files);
    }   
          
      
      $.ajax({
               url: "upload.php",
               method: "POST",
               data: str,               
               contentType: false,
               cache: false,
               processData: false,  
               beforeSend:function(){
                   $('#respimg').html("Sending...");
               },
               success:function(data){
                       $('#msgret').html(data);                       
                  }
    });
}
 

PHP

<?php
$num = 2;
if (isset($_FILES['file'])){
 for ($i = 0; $i < $num; $i++) {
    
    $nfile = $_FILES["file"]["name"][$i];
    $tfile = $_FILES["file"]["size"][$i];
    $ntemp = $_FILES["file"]["tmp_name"][$i];

}

 

It isn´t work with o if isset but without it´s giving this mensage Notice: Undefined index: file

Link to comment
https://forums.phpfreaks.com/topic/317615-upolad-file-php/
Share on other sites

This:

str.append(fileInput.files.name, fileInput.files);

Should be this:

str.append(fileInput.name, fileInput.files[i]);

You want the name of the form field to used, and the value needs to be a single file not the whole collection.

 

Lastly, your input needs to have the name:

name="file[]"

So that PHP will accept it as an array.

 

Link to comment
https://forums.phpfreaks.com/topic/317615-upolad-file-php/#findComment-1614033
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.