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

时间:2016-06-01 12:56:18

标签: javascript php jquery html image

我写了一个用于上传图片的php脚本。我想在我的所有页面中使用此脚本。问题是服务器端的输入文件<input type="file" name="input_name">的名称。我使用不同的输入名称,所以我不知道如何检测服务器端的输入名称?

Html代码:

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

JS代码:

  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:我的PHP代码:

<?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 个答案:

答案 0 :(得分:0)

<?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 :(得分:0)

您可以使用$_FILES['my_input_name']访问$_GET$_POST