0

I have an array $_FILES it can store multiple images.

When I use print_r($_FILES) after storing two images in this array it gives me following output:

Array ( [IMG_20170304_224950_jpg] => Array ( [name] => IMG_20170304_224950.jpg [type] => image/jpeg [tmp_name] => C:\wamp64\tmp\php4584.tmp [error] => 0 [size] => 4098228 ) [IMG_20170305_075610_jpg] => Array ( [name] => IMG_20170305_075610.jpg [type] => image/jpeg [tmp_name] => C:\wamp64\tmp\php4602.tmp [error] => 0 [size] => 5314040 ) )

In this I want to echo both of two images.


This is how my code looks like:

index.php

     <input id="avatar" type="file" name="avatar[]" multiple />
     <button id="upload" value="Upload" type="button">upload</button> 
    <div class="preview">
    </div>

   <div class="return_php"></div>

  <script   src="https://code.jquery.com/jquery-3.1.0.min.js" ></script>
 <script>
 $(document).ready(function(){
    var form_data = new FormData(); 
    var number = 0;

    /* WHEN YOU UPLOAD ONE OR MULTIPLE FILES */
    $(document).on('change','#avatar',function(){
        console.log($("#avatar").prop("files").length);
        len_files = $("#avatar").prop("files").length;
        for (var i = 0; i < len_files; i++) {
            var file_data = $("#avatar").prop("files")[i];
            form_data.append(file_data.name, file_data);
            number++;
            var construc = '<img width="200px" height="200px" src="' +  window.URL.createObjectURL(file_data) + '" alt="'  +  file_data.name  + '" />';
            $('.preview').append(construc); 
        }
    }); 

    /* WHEN YOU CLICK ON THE IMG IN ORDER TO DELETE IT */
    $(document).on('click','img',function(){

        var filename = $(this).attr('alt');
        var newfilename = filename.replace(/\./gi, "_");
        form_data.delete($(this).attr('alt'))
        $(this).remove()

    });

    /* UPLOAD CLICK */
    $(document).on("click", "#upload", function() {
        $.ajax({
                    url: "target.php",
                    dataType: 'script',
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: form_data,                         // Setting the data attribute of ajax with form_data
                    type: 'post',
                    success: function(data){
                        $('.return_php').html(data);
                    }
           })
    })
});

target.php

<?php


 var_dump($_FILES);
 print_r ($_FILES);

foreach ($array as $key => $image) {
  /* Now you can get each image name */
 echo $image['name'];
 }
 ?>
2
  • 1
    but what are you want to print? the image name? a <img tag with images files? Commented Sep 9, 2018 at 10:48
  • i want to print image name Commented Sep 9, 2018 at 10:56

2 Answers 2

1

You can try this code to print the image name:

foreach ($array as $key => $image) {
  /* Now you can get each image name */
  echo $image['name'];
}

EDIT: This is a upload file example

index.html

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <label for="fileToUpload">Select image to upload:</label>
    <input type="file" name="uploadFile" id="fileToUpload">
    <input type="submit" value="Upload File" name="submit">
</form>

upload.php

if (isset($_FILES['uploadFile'])) {
  print "Your file information:";
  var_dump($_FILES['uploadFile']);
} else {
  print "You need to upload a file.";
}
Sign up to request clarification or add additional context in comments.

9 Comments

No problem, glad to help you if i can.
what if i want to upload the image?
You need to do a HTML form with a <input type="file" name="..." /> and get de upload image in your php with the $_FILES array. Try to search "upload file with php" in google, it's easy.
are you here sir? if have seen this comment plz see my code below and tell me how can i upload file cause i am not able to use input name. i don't understand ajax so much ,should i use form_data instead of avatar
You don't need to use Ajax, try to add a <input type="file" name="uploadFile" /> in your form and then, in your php code you can access to file with $_FILES['uploadFile'], put a var_dump($_FILES); to see the variables.
|
0

you can try this

foreach($array as $val)
{ 
    echo $val['name'];
}

1 Comment

This may be an answer the question. But I agree that the question itself is not clear and lacks basic php knowledge. Please explain in details why your solution might work for @nitin-verma and also try to explain what print_r does and what $_FILES is. All this information could help him understand what went wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.