Safari sends $_FILE as empty (NULL) via Ajax

Asked

Viewed 78 times

0

I have an image upload script that works normally with Chrome, EDGE, Firefox and Opera. But it doesn’t work with Safari. The Safari browser opens the windows window for me to choose an image. When it should upload, it sends the data of the chosen image as NULL (empty). I’ve read several topics and articles, but none of them solved my problem.

Can someone help me understand where the mistake is?

In PHP you have the following:

    <form id="formImg" style="cursor: pointer;" enctype='multipart/form-data' method="post">
      <input type="file" id="fileUpload" name="fileUpload[]" onchange="saveImg()" onClick="" accept='image/*' >
   </form>

I have tried using the following TAG form action:"javascript:;" accept-charset="utf-8"; that I read in an article. But nothing changes.

You have the following:

function saveImg()
  {

        $('#formImg').ajaxSubmit({
        url  : 'upload.php',
        type : 'POST',
     // async: false,
      cache: false,
      contentType: false,
      processData: false,
      headers: { 'cache-control':'no-cache' },
      success: function (response) {

           if (response === "OK") {
                location.reload();
            } else {
                alert (response);
            }
         }  

     });

I have used with and without cache attributes, contenttype etc. Nothing worked. I also put in the head of html the following:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

I read in other articles that this cache problem could interfere. But that hasn’t changed anything either. Neither with, nor without. It still doesn’t work in Safari.

Finally, in the php that processes the data (upload.php), I put a small code that allows me to always know if it is sending the data as NULL (empty),

    if($_FILES['fileUpload']==NULL){
  echo "Vazio";
  exit;}

and in all attempts, it always returns "Empty". In other browsers, it works normally.

Can anyone help? I don’t know what to change in this code to work on Safari. Thank you!!!

  • tries to change your id to any other name and checks if it returns null

  • I had tried this too, put a different id, but keeps giving the same error. Works with all browsers, except with Safari.

1 answer

0

I managed to solve it. If someone is also with this problem, follow below the solution I found.

I changed js to the next:

function saveImg() {

        var myForm = document.getElementById('formImg');
        formData = new FormData(myForm);

    $.ajax({
        url  : 'upload.php',
        type : 'POST',
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        success: function (response) {
            if (response === "OK") {
                location.reload();
            } else {
                alert (response);
            }
        }  

    });
};

With this, sending worked on all browsers (including Safari) within windows.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.