what do i need todo to create a mvc media uploader that can upload more than one image..…
- User
what do i need todo to create a mvc media uploader that can upload more than one image.. using yii2
- Assistant
- User
i always thought that you builld the code handling logic in the model
- Assistant
- User
i wanna use my old code:
public function upload(): bool { if ($this->validate()) { $imageName = $this->_mediaNamePrefix . $this->imageFile->baseName .'-'. time(); //$imageName = crypt($this->imageFile->baseName, 'st') .'-'. time(); $image = $this->getUploadPath() . $imageName . '.' . $this->imageFile->extension; $transaction = \Yii::$app->db->beginTransaction(); try { if (!$this->imageFile->saveAs($image)) { throw new \yii\base\Exception("Failed to save image to path."); } $thumbnail = $this->getThumbnailPath() . $imageName . '.' . $this->imageFile->extension; ImagineImage::thumbnail($image, $this->_mediaThumbnail, $this->_mediaThumbnail) ->save($thumbnail, ['quality' => 80]); // Handled in the Media Controller - $data = new Media(); $this->base_name = $this->getUploadPath(); $this->name = $imageName . '.' . $this->imageFile->extension; $this->type = $this->imageFile->type; $this->size = $this->imageFile->size; // skip validation as model is already validated. if (!$this->save(false)) { throw new \yii\db\Exception("Failed to save media data to the database."); } // If all saves are successful, commit the transaction. $transaction->commit(); return true; } catch (\Throwable $e) { // An error occurred; roll back the transaction. $transaction->rollBack(); $this->handleException($e); } return false; } } - Assistant
- User
does the view need to change?
- Assistant
Yes, the view will need to change slightly in order to accommodate multiple file uploads. Specifically, you will need to ensure that the file input in the form allows for multiple files to be uploaded and that the corresponding model property is set to handle an array of files.
Here's how you can modify your view:
Updated View File
Assuming your view file is located at
views/site/upload.php, you should make the following changes: - User
i added the fileInput mutiple true.. but still getting only allowed one file at a time.
- Assistant
- User
- Assistant
- User
what else would cause this message? "You can upload at most 1 file."
- Assistant
- User
only rule i have set is :
[['imageFiles'], 'image', 'extensions' => 'jpg, jpeg, gif, png', 'maxSize' => 10 * 1024 * 1024],
- Assistant
- User
i dont understand the issue.
- Assistant
- User
ok, changing the rule worked. but now i dont know whats going on but nothing is uploading.
- Assistant
- User
see anything wrong with this code:
public function actionIndex(): Response|string { try { $model = new Media2(); } catch (InvalidArgumentException $e) { throw new \yii\web\BadRequestHttpException($e->getMessage()); } $request = \Yii::$app->request; if ($request->isPost && $model->load($request->post())) { $model->imageFiles = UploadedFile::getInstance($model, 'imageFiles'); if ($model->upload()) { //return $this->redirect(['view', 'id' => $model->id]); return $this->redirect(['index']); } else { $msg = \Yii::t('app', 'Please select a file to upload.'); \Yii::$app->session->setFlash('error', $msg); } } return $this->render('index', [ 'model' => $model, ]); } public function upload(): bool { if ($this->validate()) { // Was a image selected? $imageFiles[]. if (!$this->imageFiles) { return false; } $transaction = \Yii::$app->db->beginTransaction(); try { foreach ($this->imageFiles as $file) { $imageName = $this->_mediaNamePrefix . $file->baseName .'-'. time(); $image = $this->getUploadPath() . $imageName . '.' . $file->extension; if (!$file->saveAs($image)) { throw new \yii\base\Exception("Failed to save image to path."); } $thumbnail = $this->getThumbnailPath() . $imageName . '.' . $file->extension; ImagineImage::thumbnail($image, $this->_mediaThumbnail, $this->_mediaThumbnail)->save($thumbnail, ['quality' => 80]); // Handled in the Media Controller - $data = new Media(); $this->base_name = $this->getUploadPath(); $this->name = $imageName . '.' . $file->extension; $this->type = $file->type; $this->size = $file->size; if (!$this->save(false)) { throw new \yii\db\Exception("Failed to save media data to the database."); } } $transaction->commit(); return true; } catch (\Throwable $e) { $transaction->rollBack(); $this->handleException($e); } return false; } return false; } protected function handleException(\Throwable $e): void { $errorId = uniqid('model_form_error_', true); $errorData = ErrorDataHelper::createErrorData($e, $this); if ($e instanceof \yii\base\Exception) { $errorData['yiiSpecificInfo'] = 'Found a Yii-specific exception'; } LogHelper::error($errorId, $errorData); } - Assistant
- User
ok, so its uploading. but only one file.
- Assistant
- User
what if this is the update section the controller.. and only want the option for the user to be able to upload one file.
- Assistant
- User
could i not use scenario
- Assistant
- User
so create and upload more then 1 and update can only upload 1.
- Assistant