简体   繁体   English

Ajax 文件上传适用于 xampp 但不适用于在线服务器

[英]Ajax File Upload works with xampp but not on online server

I have 2 HTML pages.我有 2 个 HTML 页面。 One with a form for add a post (in this case i have an input type file), and one with a form for update a post (in this case i haven't an input type file).一个带有用于添加帖子的表单(在这种情况下我有一个输入类型文件),另一个带有用于更新帖子的表单(在这种情况下我没有输入类型文件)。 Both forms have the button with same id to do an ajax call.两种形式都有一个 id 相同的按钮来进行 ajax 调用。 The PHP file is the same for both the forms, and it insert or update tha data of a post.两种形式的 PHP 文件都是相同的,它插入或更新帖子的数据。 The js file gets the values of user's input and does the ajax call. js 文件获取用户输入的值并执行 ajax 调用。

HTML (add post) HTML(添加帖子)

<form id="form">
   <input id="titleID" type="text" value="" required />
   <input type="file" id="inputVideo" required />
   <input type="submit" id="submitData" value="SEND DATA" />
</form>

HTML (update post. It's another page. So same id it's ok) HTML(更新帖子。这是另一个页面。所以相同的 id 没关系)

<form id="form">
   <input id="titleID" type="text" value="SomeTitleValue" />
   <input type="hidden" id="idHidd" value="SomeIdValue" />
   <input type="submit" id="submitData" value="SEND DATA" />
</form>

JS (get input values and do ajax call to php file) JS(获取输入值并对php文件进行ajax调用)

$(document).ready(function() {
   $(document).on('submit', '#form', function(e) {
      e.preventDefault();

      // get values of inputs and create a formData object
      // if the value is undefined, append in formData a empty string; else append the value

      // Get id
      if ( $('#idHidd').val() != undefined ){
         var id= $('#idHidd').val();
         formData.append('id', id);
      } else {
         var id= ""
         formData.append('id', id);
      }

     // Get file video
     if ( $('#inputVideo')[0] != undefined ){
         var video= $('#inputVideo')[0].files[0]
         formData.append('video', video);
     } else {
         var video =""
         formData.append('video', video);
     }

     // Get title (always present)
     var title= $('#title').val();
     formData.append('title', title);


     // AJAX Call
     $.ajax({
        type: "POST",
        url: "urlToPHP", 
        dataType: "JSON",   
        processData: false,
        contentType: false,
        cache: false,
        data: formData,
        success: function(msg) { 
                    alert(msg)
        }
      });
   });
});

PHP PHP

//include connect db   

//Get tite and id 
$id= $_POST['id'];
$title= $_POST['title'];

// if there isn't an id, i do an insert and get the video in $_FILES
if ($id == ""){
    $video= $_FILES['video'];

    // DO QUERY FOR INSERT NEW POST AND MOVES THE FILE ON SERVER
}

if ($id != ""){

    // DO QUERY FOR UPDATE A POST
}

THE PROBLEM : in local with xampp, everything works.问题:在本地使用 xampp,一切正常。 I can insert new post and i can do an update.我可以插入新帖子,我可以进行更新。 On online server, it works only the update.在在线服务器上,它仅适用于更新。 When i do an insert, in the log_err i have the message that the indices 'id', 'video', and 'title' are undefined.当我进行插入时,在 log_err 中,我收到一条消息,即索引“id”、“video”和“title”未定义。 Like if $_POST and $_FILES were empty.就像 $_POST 和 $_FILES 是空的一样。 But in the console (with console.log), i can see that the formData object is good, and all the values are appended perfectly with all the indices.但是在控制台(使用 console.log)中,我可以看到 formData 对象很好,并且所有值都完美地附加了所有索引。 Infact, the update works, and the data are the same of an insert, except the file video.事实上,更新有效,数据与插入相同,除了文件视频。 Perhaps it's because in the insert there is a input type 'file'?也许是因为在插入中有一个输入类型“文件”? But with xampp it works.但是使用 xampp 它可以工作。 I have also tried to disable mod_security from CPanel.我还尝试从 CPanel 禁用 mod_security。 But it doesn't work the same.但它不一样。 does anyone have any solution?有没有人有任何解决方案?

Ps: sorry if code it's not perfect, but i had to do a short version for a better comprension. Ps:对不起,如果代码不完美,但我不得不做一个简短的版本以获得更好的理解。

Ok i solved my problem.好的,我解决了我的问题。

The problem was the size of the file.问题是文件的大小。 I had changed it on xampp , but not on the server.我已经在 xampp 上更改了它,但没有在服务器上更改。

I thought that a error regarding the size of a file was reported automatically, without the need of a check.我认为有关文件大小的错误是自动报告的,无需检查。 Instead, without a specific check about the size of file, i have only the error of the undefined indices相反,没有对文件大小进行特定检查,我只有未定义索引的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM