Got it working! 🍎

This commit is contained in:
Chris Smith
2025-02-19 21:43:44 +01:00
parent 658718ca06
commit 61e85ae9a2
20 changed files with 651 additions and 1200 deletions

View File

@@ -0,0 +1,41 @@
<?php
namespace frontend\models;
use Ramsey\Uuid\Uuid;
use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
class MealForm extends Model
{
/**
* @var UploadedFile
*/
public $picture;
public string $filepath;
public function rules() {
return [
[['picture'], 'file', 'skipOnEmpty' => false],
[['picture'], 'required'],
];
}
public function newFileName()
{
$this->filepath = (string)'uploads/' . Yii::$app->user->id . '-' . Uuid::uuid4() . '.' . $this->picture->extension;
}
public function upload()
{
if ($this->validate()) {
$this->newFileName();
$this->picture->saveAs($this->filepath);
return true;
} else {
return false;
}
}
}