E
E
EVOSandru62016-12-07 12:55:57
Yii
EVOSandru6, 2016-12-07 12:55:57

How to feed image models in yii without a form?

Good afternoon,
I ran into a problem when parsing a list of data. The data contains links to image folders that must be loaded along with the data.
If you use the following construction:

@file_put_contents($pathLocale, file_get_contents($pathRemote));

Then the files will be loaded, but in addition it will be necessary to register a bunch of logic for changing the size of the thumbnail, etc.
I would like the images to be $model->save()loaded in the same way as it happens when the element is loaded through the form. Because the behavior for the main picture and the behavior for additional ones are being worked out. images (in particular, the afterSave() behavior method ).
The data itself is successfully saved, but the pictures are not thrown. Maybe I incorrectly registered the data in $_FILES ? OS linux mint
Tried like this:
''''
           $this->uploadImages($images, $bust);
            //  My::printArr($_FILES);
            //  My::printArr($_POST);
            if(!$model) {
                $model  =   new Objects();
            }
            $model->setAttributes($excel, true);
            if(!$model->save())
                echo 'Ошибка сохранения '. My::printArr($model->printErrors());
}

private function uploadImages($images, $path) {
        // foreach ((array)$_POST["prev_image_ids"] as $md5) {
        if(!$images){
            return;
        }
        foreach ($images as $image) {
            $md5    =   md5(microtime()); // basename($md5);
            $imagePath  =   $path . '/' . $image;
            $tmpName     =   "/tmp/{$md5}"; //"./already-uploaded/{$md5}";
            //  $img        =   'http://static2.t-ru.org/logo/logo.gif';
            $getInfo    =   getimagesize($imagePath);
            // My::printArr($getInfo);
            //  header('Content-type: ' . $getInfo['mime']);
            //  readfile($img);
            // die();
            $_FILES['Objects']["pictures"][]   =   [
                "tmp_name"  =>  $imagePath, // $tmpName,
                "size"      =>  filesize($imagePath),   // $tmpName
                "type"      =>  $getInfo['mime'],       // image/png
                "name"      =>  $image,                 // Screenshot_20161117_141028.png
                "error"     =>  UPLOAD_ERR_OK,
            ];
        }
        $_FILES['Objects']["photo"]    =   $_FILES['Objects']["pictures"][0];
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lxfr, 2016-12-07
@lxfr

Try using Image Upload Behavior.
This behavior is connected to the model, google on this topic.
For example,
www.elisdn.ru/blog/41/usage-of-behaviors-in-yii

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question