rand_triest
rand_triest

Reputation: 135

Yii2 UploadedFile::saveAs() is not saving file

Trying to upload file:

public function uploadFile(UploadedFile $file)
{
    //  $this->image = $file;
    $file->saveAs(Yii::getAlias('@web').'uploads/'.$file->name);
    var_dump($file);
    die();
}

But the file does not appear in the destination directory.

Dump:

object(yii\web\UploadedFile)#150 (5) { ["name"]=> string(12) "IMG_0005.jpg" ["tempName"]=> string(14) "/tmp/phpDFnzQp" ["type"]=> string(10) "image/jpeg" ["size"]=> int(877612) ["error"]=> int(0) }

Upvotes: 1

Views: 2761

Answers (1)

rob006
rob006

Reputation: 22174

@web alias points o base URL of app. If you want to save file on disk, you need to use file path instead of URL - you can find it in @webroot alias:

public function uploadFile(UploadedFile $file) {
    return $file->saveAs(Yii::getAlias('@webroot') . '/uploads/' . $file->name);
}

Upvotes: 3

Related Questions