未定义的索引上传

时间:2019-07-31 21:48:23

标签: javascript php jquery ajax file-upload

为什么向我显示此错误:“未定义索引:上传”?

我找到了很多与此主题相关的帖子,并修改了我的代码,但是它不起作用

HTML:

<form method="POST" class="formphoto" enctype="multipart/form-data">

<input type="file" name="upload" class="inputfile" id="file1">

Ajax

$('.inputfile').on('change', function(event) {

// var upload = event.target.value;

$.ajax({
    type: 'POST',
    method: 'POST',
    url: '../MeetBlog/pages/upload_cache/upload_cache.php',
    data: new FormData($(".formphoto")[0]),
    cache: false,
    contentType: false,
    processData: false,
    success: function(data) {
        $('.imgshow').html(data);
    },
    });
}); 
});

PHP

<?php
session_start();
require_once __DIR__ . '/../../config/connect.php'; 

var_dump($_FILES['upload']['tmp_name']);

?>

这到服务器了:

This comes to the Server

1 个答案:

答案 0 :(得分:0)

问题出在您的php代码中,我已经测试了您的html代码,但您的代码中还有一个})

然后我在自己的php代码中print_r $_FILES,效果很好。

[2019-08-01 08:20:34][INFO] Files:--->: Array
(
    [upload] => Array
        (
            [name] => test.html
            [type] => text/html
            [tmp_name] => /tmp/php4ZbAqB
            [error] => 0
            [size] => 647
        )

)

$('.inputfile').on('change', function(event) {
// var upload = event.target.value;
$.ajax({
    type: 'POST',
    method: 'POST',
    url: 'https://example.com/upload_cache.php',
    data: new FormData($(".formphoto")[0]),
    cache: false,
    contentType: false,
    processData: false,
    success: function(data) {
        $('.imgshow').html(data);
    },
    });
}); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST" class="formphoto" enctype="multipart/form-data">

<input type="file" name="upload" class="inputfile" id="file1">