Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过ajax上载文件,但返回未定义的索引_Php_Ajax - Fatal编程技术网

Php 通过ajax上载文件,但返回未定义的索引

Php 通过ajax上载文件,但返回未定义的索引,php,ajax,Php,Ajax,当我将文件上传到Ajax时,它可以工作,但当文件传回PHP时,它显示未定义索引 <html> <textarea name="recipe_preparation[]"></textarea><input type="file" name="preparation_photo[]" /> <textarea name="recipe_preparation[]"></textarea><input type="

当我将文件上传到Ajax时,它可以工作,但当文件传回PHP时,它显示未定义索引

 <html> 
 <textarea name="recipe_preparation[]"></textarea><input type="file" name="preparation_photo[]" />
 <textarea name="recipe_preparation[]"></textarea><input type="file" name="preparation_photo[]" />
</html>

这是关于我提交文件的java脚本

<script>
  var len=$("[name='preparation_photo[]']").length;
   for ( var i=0; i < len; i++ ) 
  {

  formdata.append("file", document.getElementsByName("preparation_photo[]")[i].file);

}

$.ajax({
    type : "POST",
    url : "add.php",
    data : formdata,
    processData: false,
    contentType: false,
    success : function(data){
        alert(data);
    }

});
<script>

<?php
 $target_path = "img/";
for($i=0; $i<count($_FILES['file']['name']); $i++){
$ext = explode('.', basename( $_FILES['file']['name'][$i]));
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1]; 

if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
    echo "The file has been uploaded successfully <br />";
} else{
    echo "There was an error uploading the file, please try again! <br />";
?>

var len=$(“[name='preparation_photo[]']”)长度;
对于(变量i=0;i
使用混合变量名

在HTML中有
name=“preparation\u photo[]”

在PHP
$\u文件['file']


切换到
$\u FILES['preparation\u photo']

它仍然是一样的。我可以提醒get object元素,但不知道为什么仍然没有定义索引