M
M
Millerish2018-03-12 16:08:00
PHP
Millerish, 2018-03-12 16:08:00

React: How to upload an image correctly (php)?

I am using react with php. How to upload an image correctly?
I try this (I get File is not an image.):
php:

$target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
                $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
                if($check !== false) {
                    echo "File is an image - " . $check["mime"] . ".";
                    $uploadOk = 1;
                } else {
                    echo "File is not an image.";
                    $uploadOk = 0;
                }

javascript:
constructor(props) {
        super(props)
        $(document).ready(function (e){
            console.log(1)
            $("#uploadForm").on('submit',(function(e){
                console.log(2)
                e.preventDefault();
                $.ajax({
                    url: "https://worldwideshop.ru/vbot-dev/api/?command=upload_img&file_name=1.jpeg",
                    type: "POST",
                    data:  new FormData(this),
                    contentType: false,
                    cache: false,
                    processData:false,
                    success: function(data){
                        // $("#targetLayer").html(data);
                        console.log(data)
                    },
                    error: function(){
                        console.log(4)
                    }
                });
            }));
        });
    }

html:
<form id="uploadForm" method="post">
                            <label>Upload Image File:</label><br/>
                            <input name="userImage" type="file" class="inputFile" />
                            <input type="submit" value="Submit" class="btnSubmit" />
                        </form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2018-03-22
@iCoderXXI

OnSubmit={this.submitHandler} is hung on the form tag, into which the evt argument arrives. Through evt we get access to userImage. You can send via axios.
To suffer such garbage in the constructor, and even with jQuery, this is a very suitable perversion. :)
In general, PHP with React is not a very combination, as soon as it comes to Server Side Rendering in the name of Seo, here you will understand how great you missed with puff on the back.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question