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条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装