S
S
Sergey Beloventsev2016-05-04 20:39:36
Yii
Sergey Beloventsev, 2016-05-04 20:39:36

How to save a file from a third-party site?

parsers.php model

use common\models\UploadImage;
use yii\web\UploadedFile;
use dosamigos\transliterator\TransliteratorHelper;
$image=new Image();
        $imageUrl=$informayion['poster_url'];
        $images = file_get_contents(http://trinixy.ru/pics5/20121108/awesome_40.jpg);
        $files= new UploadImage();
        $files->files = UploadedFile::getInstance($files,$images);
        $files_to = TransliteratorHelper::process($files->files->name, '', 'en');
        $path = 'serial';
        $image->id_serial=$theIdsSerial;
        $years=date('Y');
        $mounts=date('m');
        if ($files->upload($path,$years,$mounts,$files_to)) {
            $image->path = $path.'/'.$years.'/'.$mounts.'/';
            $image->name = $files_to;
            $image->save();
        }else{
            print_r($files->getErrors());
        }

model UploadImage.php
<?php

namespace common\models;
use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
class UploadImage extends Model
{
    public $files;
    public function rules()
    {
        return [
            [['files'], 'file','skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif'],
        ];
    }
    public function upload($path,$years,$mounts,$files_to){
        if ($this->validate()) {
            if (file_exists(Yii::getAlias('@frontend/web/image/').$path.'/'.$years.'/'.$mounts.'/')) {
                } else {
                    mkdir(Yii::getAlias('@frontend/web/image/').$path.'/'.$years.'/'.$mounts.'/', 0775, true);
                }
                $this->files->saveAs(Yii::getAlias('@frontend/web/image/').$path.'/'.$years.'/'.$mounts.'/'.$files_to);
            return true;
        } else {
            return false;
        }
    }

}

as a result, it says Call to a member function saveAs() on a non-object, I assume that this is an error
$images = file_get_contents(http://trinixy.ru/pics5/20121108/awesome_40.jpg);
$files->files = UploadedFile::getInstance($files,$images);

please tell me how to do it right.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
R
Rustamka Vorontsov, 2016-05-04
@Sergalas

You asked yesterday, I sent you 3 options How to upload a picture from the link?
what exactly is the problem?
install the package https://github.com/Intervention/image
then you just give the url of the third party site
image $image = Image::make(' trinixy.ru/pics5/20121108/awesome_40.jpg ');
and save
$image->save('/path/to/awesome_40.jpg');
Well, or there are 3 more options written.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question