0

In my index.php I have a form where a file upload will happen

 <form id="uploadImage" method="post" action="upload.php">

                                    <span class="input-group-text">

                                        <label for="attach-doc" class="attachment-icon mb-0">

                                            <i data-feather="image" class="cursor-pointer lighten-2 text-secondary"></i>

                                            <input type="file" name="uploadFile" id="uploadFile" accept=".jpg, .png" /> </label></span>
                                    </form>

I have a jquery for ajax submit as

$('#uploadFile').on('change', function(){

    $('#uploadImage').ajaxSubmit({
    // target: "#editor1 .ql-editor",
    // resetForm: true
    });
 });

And my upload.php code is

<?php

//upload.php



if(!empty($_FILES))
{
    if(is_uploaded_file($_FILES['uploadFile']['tmp_name']))
    {
        $ext = pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION);
        $allow_ext = array('jpg', 'png');
        if(in_array($ext, $allow_ext))
        {
            $_source_path = $_FILES['uploadFile']['tmp_name'];
            $target_path = 'upload/' . $_FILES['uploadFile']['name'];
            if(move_uploaded_file($_source_path, $target_path))
            {
                echo '<p><img src="'.$target_path.'" class="img-thumbnail" width="200" height="160" /></p><br />';
            }
            //echo $ext;
        }
    }
}

?>

File locations are :File Location

So as per the code when I click on the upload file a ajax call should happen and the image should be uploaded to the folder upload .

But upload is not happening with the above code, Any help appreciated.

11
  • Are there any errors in the console? Did you include the jQuery Form Plugin (because ajaxSubmit() is not a standard jQuery method) correctly? Commented Jul 22, 2021 at 7:14
  • No errors in the console. Commented Jul 22, 2021 at 7:16
  • 1
    We're going to need some more specifics than "not happening". Have you done any proper debugging? Commented Jul 22, 2021 at 7:18
  • What debugging have you done? Have you checked the network tab (in the browsers dev tools) if a request is made, what data (if any) it sends and what the response is? Have you checked the web servers error log for PHP errors? Have you debugged to see if $_FILES are empty or not? Have you checked the value of $_FILES['uploadFile']['error']? Please do some basic debugging and report your findings. Commented Jul 22, 2021 at 7:19
  • Basically Image is not uploading to the upload folder Commented Jul 22, 2021 at 7:19

1 Answer 1

1

You have missed one form attribute enctype="multipart/form-data" that is used for file uploading.

If the form tag does not have this attribute, then your file will not be uploaded to the server.

Please change following line:

 <form id="uploadImage" method="post" action="upload.php">

to

<form id="uploadImage" method="post" action="upload.php" enctype="multipart/form-data">

I hope this will work for you

Sign up to request clarification or add additional context in comments.

1 Comment

This is irrelevant because the upload is happening via AJAX, not a standard form submission. Not sure why this was upvoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.