du9757 2018-09-09 10:42
浏览 112
已采纳

如何在使用print_r后从数组中获取单个值

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条回答 默认 最新

  • dongzhi8984 2018-09-09 10:58
    关注

    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.";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行