formData file to PHP

时间:2019-03-19 14:57:22

标签: javascript php

I'm trying to pass a image file from javascript to a handler in PHP, but I can't get the PHP file to find the file data.

This is my javascript

function browseImage(){
    (async function getImage () {
        const {value: file} = await Swal.fire({
            title: 'Select image',
            input: 'file',
            inputAttributes: {
                'accept': 'image/*',
                'aria-label': 'Upload your profile picture'
            }
        })

        if (file) {
            const reader = new FileReader
            reader.onload = (e) => {
                (async function uploadImage () {
                    const {value: accept} = await Swal.fire({
                        title: 'Your uploaded picture',
                        imageUrl: e.target.result,
                        imageAlt: 'The uploaded picture',
                        confirmButtonText:
                            'Ok <i class="fa fa-arrow-right></i>'
                    })

                    if (accept) {
                        var formData = new FormData();    // Creating object of FormData class
                        formData.append('pic[]', file);
                        var nam = formData.get('pic[]');
                        window.console && console.log(nam);
                        post('../controller/profile_controller.php', {'img': formData});
                    }
                })()
            }
            reader.readAsDataURL(file)
        }
    })()
}

Question is, where I can find in the PHP the passed image? I tried with $_POST['img'] and $_FILES['img'], also in $_FILES['img']['pic'], and in $_FILES['pic'], and $_FILES['pic[]']...

Now, for some reason, in my console.log I get this:

File {
    name: "buenos_aires.jpg", 
    lastModified: 1541160482000, 
    lastModifiedDate: Fri Nov 02 2018 09:08:02 GMT-0300 (Chile Summer Time),
    webkitRelativePath: "", 
    size: 93059, …
}

But when I use a normal Form->submit cycle in PHP, I note that the sent array has things like "type", "tmp_name" and so on, and this are things that I don´t see here. So my guess is that the formData object received by PHP is not the appropriate..

help please...

0 个答案:

没有答案