F
F
Fqwd1232021-02-15 18:39:55
Yii
Fqwd123, 2021-02-15 18:39:55

How to save an image to the server?

I can’t upload a photo to the server in any way, I did it according to various examples, the upload simply does not occur. Tell me what I'm doing wrong:
Model:

class UploadForm extends Model
{
   
    public $imageFile;

    public function rules()
    {
        return [
            ) ?>

<?= $form->field($model, 'imageFile')->fileInput() ?>

    <button>Submit</button>

<?php ActiveForm::end() ?>

As I believe the whole trouble is in this line
$this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
it does not find such a folder, I have the uploads folder right in the root, if this is the problem, then tell me how to set the path to it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Slashchinin, 2021-02-16
@Fqwd123

It may be worth specifying the full path to save.
I usually create aliases to store files

'@picture' => '/uploaded/',
'@upload' => '@app/web/uploaded/',

The first alias is for accessing the file via the Web, and the second is for saving the file. this is the full path.
This is what saving the file looks like
$file = UploadedFile::getInstance($this, $code);
$filePath = time() . '_' . $file->baseName . '.' . $file->extension;
$file->saveAs('@upload/' . $filePath);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question