-1

i have a form that uploads image files and it's not working. I have tried submit and click events, the error appears when i have removed the if statement. Thanks in advance for your help.

                <form action="index.php" method="POST" enctype="multipart/form-data" id="image-1">
                    <label class="btn btn-default" id="label-1">Upload Image
                        <input type="file" name="file" multiple="multiple" id="file-1">
                    </label>
                    <span id="save-1"></span>
                </form>


    $('#save-1').on('click', function(event){
        event.preventDefault();
        var sliderForm = $('#image-1')[0];

        $.ajax({
            url: 'image-1.php',
            method: 'POST',
            data: new FormData(sliderForm),
                contentType: false,
                cache: false,
                processData:false,
                enctype: 'multipart/form-data',
                success:function(data){
                $('.update-res').html(data);
            }
        });

    });

PHP script

if (isset($_FILES['file'])) {
echo $file = $_FILES['file']['name'];
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • `echo $file = $_FILES['file']['name'];` , quit echo – miglio Jan 21 '20 at 16:16
  • It's not working still, and with the isset statement it does not pick any thing up – Tladi Ramahata Jan 21 '20 at 16:25
  • check the post_max_size value of your server, if your post exceeds that limit, your $_FILES will be null. – user3154108 Jan 21 '20 at 16:31
  • I have already regulated it before with PHP if ($_FILES['file']['size'] > 1024) { } else { echo "File uploaded is too large, upload file size should not be larger than 1 Mb !"; $status = true; } – Tladi Ramahata Jan 21 '20 at 16:38
  • 1
    Does this answer your question? [jQuery AJAX file upload PHP](https://stackoverflow.com/questions/23980733/jquery-ajax-file-upload-php) <- Note how they are using FormData and then append the files to the object and not just passing the input field directly to the constructor – M. Eriksson Jan 21 '20 at 16:41

1 Answers1

0
<form action="index.php" method="POST" enctype="multipart/form-data" id="image-1">
   <label class="btn btn-default" id="label-1">Upload Image
       <input type="file" name="file"  multiple="multiple" id="file-1">
   </label>
   <span id="save-1"></span>
</form>

Try this code new FormData(document.getElementById("file-1"))

    $('#save-1').on('click', function(event){
        event.preventDefault();
        var sliderForm = $('#image-1')[0];

        $.ajax({
            url: 'image-1.php',
            method: 'POST',
            data: new FormData(document.getElementById("file-1")),
            contentType: false,
            cache: false,
            processData:false,
            enctype: 'multipart/form-data',
            success:function(data){
                $('.update-res').html(data);
            }
        });

    });

Similar question : PHP File Upload Undefined Index error

Zeeshan Eqbal
  • 245
  • 2
  • 11