当使用ajax上传文件时,文件上传会抛出“undefined index”错误

当使用ajax上传文件时,文件上传会抛出“undefined index”错误

问题描述:

我试图通过在php中使用ajax来上传文件,并花了一天的时间来解决这个问题。我阅读相同的问题,并试图使用它,但它没有工作。 也许它可能会混乱,因为我试图从堆栈溢出所有解决方案代码到我的代码。当使用ajax上传文件时,文件上传会抛出“undefined index”错误

它可能会很长,但PLZ帮助我。谢谢T.T

html部分。

<form id="fileform" name="fileform" enctype="multipart/form-data"> 
    <input id="fileinput" multiple="multiple" value="file" type="file" name="file"> 
    <input id="file" type="submit" value="submit"> 
</form> 

ajax part。

<script> 
$(document).ready(function(){ 

    $('form#fileform').submit(function(event){ 
    event.preventDefault(); 

    var formData = new FormData($(this)[0]); 
    $.ajax({ 
     type:"POST", 
     url:"io_update.php", 
     contentType: "application/json; charset=utf-8", 
     dataType:"JSON", 
     data: formData, 
     cache : false, 
    processData: false, 

     success:function(data){ 
     // var success_text = '<div class="alert alert-success"><strong>Upload success.</strong> Here is the link to your file:</div>' + 
       // '<span class="lead"><a target="_blank" href="https://file.io/' + data + '">https://file.io/' + data + '</a><br /><br /></span>'; 
      console.log("hh"); 
      // console.log(data); 
       // $(this.getElementById("list")).html(success_text); 
     document.getElementById("list").innerHTML ='<div class="alert alert-success" style="font-size:10px; "><strong>Upload success.</strong> Here is the link to your file:</div>' + 
        '<span class="lead"><a target="_blank" href="' + data + '">' + data + '</a><br /><br /></span>'; 
     // alert(data); 

       },error:function(request,status,error){ 
     console.log("gg"); 
alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);} 



    }) 
    }) 
}) 

php部分。

$file = json_decode($_POST['file']); 
$folder="img/"; 

move_uploaded_file($file,$folder.$file); 

function getimg($url) { 
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg'; 
    $headers[] = 'Connection: Keep-Alive'; 
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; 
    $user_agent = 'php'; 
    $process = curl_init($url); 
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($process, CURLOPT_HEADER, 0); 
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent); //check here 
    curl_setopt($process, CURLOPT_TIMEOUT, 30); 
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 
    $return = curl_exec($process); 
    curl_close($process); 
    return $return; 
} 

$imagename= basename($file); 
$image = getimg($file); 
file_put_contents('./img/'.$imagename,$image); 


$sql1="INSERT INTO a_io(file) VALUES('$imagename')"; 
$result = mysqli_query($db,$sql1); 

和错误消息是 消息:

Notice: Undefined index: file in /home/hosting_users/kaist6123/www/io/io_update.php on line 45 

Warning: file_put_contents(./img/): failed to open stream: Is a directory in /home/hosting_users/kaist6123/www/io/io_update.php on line 81 

,并在我的数据库,我把阿贾克斯后空文件。

empty file comes into database

从你的Ajax功能删除这些评论后

contentType: "application/json; charset=utf-8", 
dataType:"JSON", 

更新

尝试用替换以下行

$file = json_decode($_POST['file']); 
$file = $_POST['file']; 
+0

哦!我现在可以在ajax中输入成功的一部分,但仍然有类似上面的错误消息。但真的非常感谢你!!!!!我仍然在考虑错误问题。 @RaviMCA –