M
M
Mikhail Mikhail2015-09-06 18:59:35
PHP
Mikhail Mikhail, 2015-09-06 18:59:35

How can a form accept an image and submit it to a given URL?

Good afternoon. I still can’t send an image to VK.
I have a form where the user selects an image:

<form action="index.php" method='post' enctype="multipart/form-data">
        <fieldset>
            <label for="user_pic">Upload picture:</label>
            <input type="file" name="user_pic" >
        </fieldset>
        <fieldset>
            <input type="submit" value='Upload'>
        </fieldset>
    </form>

this is how I accept it with a script :
if ($_FILES['user_pic']) {
    $img = file_get_contents($_FILES['user_pic']['tmp_name']);
    $vk->uploadPicture('номер группы !!' , $img);
}

and here is the send function itself
public function uploadPicture($GroupID, $pictures)
    {
        /*
        *  photos.getWallUploadServer
        *Возвращает адрес сервера для загрузки фотографии на стену пользователя или сообщества.
        */
        $params = [
            'group_id' => $GroupID,
            'access_token' => $_SESSION['AccessToken']
        ];
        $Info = json_decode(file_get_contents('https://api.vk.com/method/photos.getWallUploadServer' . '?' . urldecode(http_build_query($params))), true);

        //все хорошо, получили URL
        // отправляем на полученный URL нашу картинку
        if (isset($Info['response'])) {
            $params = [
                'photo' => $pictures
            ];
            $Info = file_get_contents($Info['response']['upload_url'] . '&' . urldecode(http_build_query($params)));
            print '<p>' . var_dump($Info) . '</p>'; // возвращает NULL....

            /*
             * если уж и тут все хорошо, Сохраняем фотографии после успешной загрузки на URI
             */

Am I getting the picture from the form and sending it to the function correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artur Atnagulov, 2015-09-06
@atnartur

You need to accept a file in PHP in this way: php.net/manual/ru/features.file-upload.post-method.php
To upload it to VKontakte, you need to place the uploaded file in some temporary folder. Then you need to upload it to VKontakte. (detailed description of the process: vk.com/dev/upload_files)
After uploading the file, you will need to save it and attach it to a record or somewhere else. I hope I explained clearly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question