-1

In my html i have this: <input type="file" id="apkfile_uploader" name="apkfile_uploader_name" class="form-control-file">

and the form have action="path" enctype="multipart/form-data" method="POST"

when i am trying to upload the file using php the $_FILES returns undefined index error for this line of code: $target_file = $target_dir . basename($_FILES["apkfile_uploader_name"]["name"]); the error goes as follows:Undefined index: apkfile_uploader_name

Elias Khazzaka
  • 163
  • 2
  • 11
  • 1
    try debugging the `$_FILES` variable by var_dump it to see whether the file is actually uploaded or not – Al-Amin Oct 24 '20 at 09:25
  • I have tried ```$file= $_FILES["apkfile_uploader_name"]["name"]; echo var_dump($file);``` and it returned an error of ` Undefined index: apkfile_uploader_name` on this line `$file= $_FILES["apkfile_uploader_name"]["name"];` – Elias Khazzaka Oct 24 '20 at 09:30
  • 1
    not like that, try var_dump( $_FILES ) where you will see the array and check for "apkfile_uploader_name" . if not there then your file is not uploading in server – Al-Amin Oct 24 '20 at 09:35
  • okay , this code `echo var_dump($_FILES["apkfile_uploader_name"]["name"]);` also result in an undefined index error but it returned NULL, so no file is read? – Elias Khazzaka Oct 24 '20 at 09:38
  • 1
    you don't need to echo var_dump, just write ' var_dump( $_FILES ); ' and see don't add any array index like previous comment – Al-Amin Oct 24 '20 at 09:45
  • it returned and empty array `array(0) { }` – Elias Khazzaka Oct 24 '20 at 09:56
  • 1
    so your file is not uploading in the server , check your code – Al-Amin Oct 24 '20 at 10:11
  • thank you for your help i figured that the only thing preventing the code is js – Elias Khazzaka Oct 24 '20 at 10:16

1 Answers1

0

I first thought the problem was caused either from the form or from the php but it turned out that the js preventing the page was the cause. Here is the code that worked for me:


jQuery(document).on('submit', '#wnform_settings', function(e) {
    e.preventDefault();
    $.ajax({
        xhr: function() {
            var xhr = new window.XMLHttpRequest();
            xhr.upload.addEventListener("progress", function(element) {
                if (element.lengthComputable) {
                    var percentComplete = ((element.loaded / element.total) * 100);
                    percentComplete = percentComplete.toFixed(0);
                    $("#progressBarback").val(percentComplete);
                }
            }, false);
            return xhr;
        },
        type: 'POST',
        url: $(this).prop('action'),
        data: new FormData(this),
        contentType: false,
        cache: false,
        processData: false,
        dataType: 'text',

        beforeSend: function() {
            setTimeout(() => {
                $("#progressBarback").val(0);
            }, 1000);

        },

        success: function(json) {
            console.log(json);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(ajaxOptions + "\r\n" + thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

Elias Khazzaka
  • 163
  • 2
  • 11