douzhicong5965 2016-06-01 12:56
浏览 64
已采纳

如何检测服务器端的输入[type =“file”]名称(<input type =“file”>)?

I have written a php script for uploading images. I want to use this script in my all pages. The problem is that the name of input file <input type="file" name="input_name"> in the server side. I am using a different input name so I do not know how I detect the input name in the server side?

Html code :

<input type="file" name="my_input_name" id="inptId">

JS Code:

  var fd = new FormData();
  var image = document.getElementById("inptId");
  fd.append("my_input_name",image);
  $.ajax({
   method:post,
   url:uniq_file_uploader.php,
   ....,
   ....,
   data:fd,
   sucess:function(e){....}
  });

EG: my php code:

<?php
 if($_SERVER["REQUEST_METHOD"] === "post"){
  if(!empty($_FILES['Constant_Name']['name'][0])){//check also for multiple uploads
     // constant_name is name of input file (eg. my_input_name)
    // but problem is input name is not always same..
   // on every page it is different name

  }else {return "NO file selected";}
  if(!empty($_FILES['Constant_Name']['name'])){
    //it is for one file upload detect
    //this is for one file upload beacuse this is for using upload his avatar. can't be a lot of avatar
  }else{return "no file selected";}
 }else {return "request is not post"}
?>
  • 写回答

2条回答 默认 最新

  • duanbushi1867 2016-06-01 13:02
    关注
    <?php
     if($_SERVER["REQUEST_METHOD"] === "post"){
      if(!empty($_FILES)) {
         $cname = key($_FILES); //get the first key from $_FILES
      } else {
         $cname = '';
      }
      if(!empty($_FILES[$cname]['name'][0])){//check also for multiple uploads
         // constant_name is name of input file (eg. my_input_name)
        // but problem is input name is not always same..
       // on every page it is different name
    
      }else {return "NO file selected";}
      if(!empty($_FILES[$cname]['name'])){
        //it is for one file upload detect
    
      }else{return "no file selected";}
     }else {return "request is not post"}
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料