T
T
teodor7teodor72020-02-21 19:25:02
Yii
teodor7teodor7, 2020-02-21 19:25:02

Yii2, yii2-queue, File upload how to do?

I'm trying to load a file that is created in tmp from a temporary directory. But the file is not located or it is created in another directory. How to make a download?

//namespace frontend\controllers;

    public function actionImport()
    {
        $model = new ImportForm();

        if (!\Yii::$app->user->can('sellerAccess')) {
            throw new NotFoundHttpException('Access denied');
        }

        if (Yii::$app->request->isPost) {
            \Yii::$app->session->setSavePath('tmp');
            $model->file = UploadedFile::getInstance($model, 'file');
            if ($model->validate()) {

                Yii::$app->queue->push(new ImportFile([
                    'user_id' => Yii::$app->user->id,
                    'file' => $model->file
                ]));

                \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'File upload completed successfully'));
                return $this->render('import', [
                    'title'  => 'Import',
                    'model' =>  $model
                ]);
            }
            \Yii::$app->getSession()->setFlash('danger', \Yii::t('user', 'File upload error'));
        }
        return $this->render('import', [
            'title'  => 'Import',
            'model' =>  $model
        ]);
    }

namespace frontend\models;

use yii\base\BaseObject;
use common\models\Product;
use  Yii;

class ImportFile extends BaseObject implements \yii\queue\JobInterface
{
    public $url;
    public $file;
    public $user_id;

    public function execute($queue)
    {
       // file_put_contents($this->file, file_get_contents($this->url));
        echo "Hi";

       $path = \Yii::$app->runtimePath . $this->file->tempName . $this->file->name;
        var_dump( $path  );

            if (!file_exists($path ) || !is_readable($path )) {
           echo 1;     return false;
            }
            $data = array();
            if (($handle = fopen( $path, 'r')) !== false) {
                echo 2;
                
                }
            }

            return true;

    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question