简体   繁体   中英

Ajax File Upload works with xampp but not on online server

I have 2 HTML pages. 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. The PHP file is the same for both the forms, and it insert or update tha data of a post. The js file gets the values of user's input and does the ajax call.

HTML (add post)

<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)

<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)

$(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

//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. 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. Like if $_POST and $_FILES were empty. 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. 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. I have also tried to disable mod_security from CPanel. 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.

Ok i solved my problem.

The problem was the size of the file. I had changed it on xampp , but not on the server.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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