@teodor7teodor7

Yii2, yii2-queue, загрузка File как сделать?

Пытаюсь загрузить файл который создается в tmp из временной директории. Но файл не находится или он создается в другой директории. Как сделать загрузку?
//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;

    }
}
  • Вопрос задан
  • 123 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы