Jitendra Jagtap
Jitendra Jagtap

Reputation: 13

Move file from one directory to other directory in yii2

I have uploaded file on my shared server now I want to move file using yii2 libraries how can I move this file.

Upvotes: 1

Views: 1933

Answers (1)

zakrzu
zakrzu

Reputation: 595

Simple use this: http://php.net/manual/en/function.rename.php

or in uploadAction you can use saveAs method when you upload file like this:

public function actionUpload()
{
    $model = new UploadForm();

    if (Yii::$app->request->isPost) {
        $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
        if ($model->upload()) {
            // file is uploaded successfully
            return;
        }
    }

    return $this->render('upload', ['model' => $model]);
}


class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $imageFile;

    public function rules()
    {
        return [
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
        ];
    }

    public function upload()
    {
        if ($this->validate()) {
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
        } else {
            return false;
        }
    }
}

manual: http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html

Upvotes: 1

Related Questions