public function beforeSave($insert) { if (parent::beforeSave($insert)) { $petImage = new PetImage(); $files = UploadedFile::getInstances($petImage, 'source_url'); foreach ($files as $file) { $explodedImage = explode('.', $file->name); $extension = end($explodedImage); $fileName = 'images/pets/' . time() . '_' . rand(10000, 99999) . '.' . $extension; $file->saveAs($fileName); $petImage = new PetImage(); $petImage->source_url = $fileName; $petImage->pet_id = $this->id; $petImage->alt = ''; if (!$petImage->save()) { echo '<pre>'; var_dump($petImage->getErrors()); die; } } return true; } return false; }
public function actionRemove($id) { /** @var PetImage $model */ $model = PetImage::findOne(['id' => $id]); @unlink($model->source_url); $model->delete(); return; }