PHP code example of fabriziocaldarelli / yii2-file-upload
1. Go to this page and download the library: Download fabriziocaldarelli/yii2-file-upload library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fabriziocaldarelli / yii2-file-upload example snippets
namespace backend\models;
class Articles extends \yii\db\ActiveRecord
{
public $formTabularIndex = null; // Needed for file upload with tabular form
public $photo; // attribute to store uploaded file
// Load: initialize the files attached to model
public function load($data, $formName = null)
{
$retLoad = parent::load($data, $formName);
// Files metadata will be stored with refer_table=articles, section=articles, category=photo and refer_id=model.id
\sfmobile\fileUpload\FileUploadCore::load($this->formTabularIndex, $this, 'photo', $this->id, 'articles', 'articles', 'photo');
return $retLoad;
}
// Delete: added files deletion
public function afterDelete()
{
parent::afterDelete();
// When delete the model, files automatically will be deleted
\sfmobile\fileUpload\FileUploadCore::deleteAll($this->id, 'articles', 'articles', 'photo');
}
// Save: after saved the model, also save the files
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
// File upload
\sfmobile\fileUpload\FileUploadCore::sync($this, 'photo', \Yii::$app->user->identity->id, $this->id, 'articles', 'articles', 'photo');
}
public function rules()
{
return [
// ...
// minimum 2 files and maximum 5 files
['photo', 'file', 'minFiles' => 2, 'maxFiles' => 5 ],
];
}
public function actionCreate()
{
$model = new Articles();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
public function actionUpdateMultiple($ids)
{
$models = \common\models\Articles::find()->andWhere(['id' => explode(',', $ids)])->all();
foreach($models as $indexModel => $model)
{
$model->formTabularIndex = $indexModel;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
}
return $this->render('update-multiple', [
'models' => $models,
]);
}
class BannerHomeIndexForm extends \common\models\BannerHome
{
public $fileInputIndexName = null; // Extra parameter to specify which $_FILES index to use
public $formTabularIndex = null; // Needed for file upload with tabular form
public $image;
// Load: initialize the files attached to model
public function load($data, $formName = null)
{
$retLoad = parent::load($data, $formName);
// !!! ATTENTION TO LAST PARAMETER !!!!
\sfmobile\fileUpload\FileUploadCore::load($this->formTabularIndex, $this, 'image', $this->id, 'banner_home', 'banner_home', 'image', $this->fileInputIndexName);
return $retLoad;
}
// Delete: added files deletion
public function afterDelete()
{
// ... as default example ...
}
// Save: after saved the model, also save the files
public function afterSave($insert, $changedAttributes)
{
// ... as default example ...
}
public function actionIndex()
{
if(\Yii::$app->request->isPost)
{
foreach($_FILES['BannerHomeForm']['name']['image'] as $indexBanner => $n)
{
$model = new BannerHomeForm();
$model->formTabularIndex = $indexBanner;
$model->fileInputIndexName = 'BannerHomeForm[image]['.$indexBanner.']';
if ($model->load(Yii::$app->request->post()) && $model->save()) {
}
}
}
\sfmobile\fileUpload\FileUploadCore::destroySession();
php composer.phar
"fabriziocaldarelli/yii2-file-upload": "*"
@param $options array of [ 'filter' => [ 'andWhere' => [] ], 'saveFields' => [ 'array of fields' ] ]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.