Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.
Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin. Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Yii2 Gallery yaratish

0 ovoz
69 marta ko‘rilgan
so‘radi 08 yanvar Hannibal (3 bal)
Assalomu aleykum, Yii framework uchun gallery izlab shu scriptni yozdim ammo bazaga yozmayapti. Rasmni yuklagincha saveAs () no null, deyatotgan edi. To'liq yozib bo'lgandan keyin xatolik bermayapti ammo bazaga yozmayapti.
Mediacontroller.php
<?php

namespace app\controllers;

use Yii;
use app\models\Media;
use yii\web\UploadedFile;
use yii\web\controller;

class MediaController extends Controller
{
    public function actionIndex()
    {
        $date = Media::find()->all();
        return $this->render('index', ['medias'=>$date]);
    }

    public function actionUpload()
    {
        $model = new Media();

        if ($model->load(Yii::$app->request->post())) {
            if ($model->validate()) {
                // form inputs are valid, do something here
                $names = UploadedFile::getInstances($model, 'filename');
                foreach ($names as $name) {
                    $path = 'uploads/'.md5($name->baseName).'.'.$name->extension;
                    if ($name->saveAs($path)){
                        $filename = $name->baseName.'.'.$name->baseName;
                        $filepath = $path;
                        Yii::$app->db->createCommand()->insert('media',['filename'=>$filename, 'filepath'=>$filepath])->execute();

                    }
                }
                return $this->redirect(['index']);
            }
        }

        return $this->render('upload', [
            'model' => $model,
        ]);
    }
}
Media.php modul
<?php

namespace app\models;
use yii\db\ActiveRecord;


/**
 * This is the model class for table "media".
 *
 * @property int $id
 * @property string|null $image
 */
class Media extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'media';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['filename'], 'file', 'maxFiles' => 10],
            [['filepath'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            //'id' => 'ID',
            'filename' => 'filename',
        ];
    }
}
View/media/upload.php
<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$this->title = "Uploads file";
$this->params['breadcrumbs'][]= $this->title;

/* @var $this yii\web\View */
/* @var $model app\models\Media */
/* @var $form ActiveForm */
?>
<div class="media-upload">
    <h1><?php echo Html::encode($this->title)?></h1>
    <?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-date']]); ?>

        <?= $form->field($model, 'filename[]')->fileInput(['multiple'=>true, 'accept'=>'image/*']) ?>
    
        <div class="form-group">
            <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
        </div>
    <?php ActiveForm::end(); ?>

</div><!-- media-upload -->
View/media/index.php
<?php
/* @var $this yii\web\View */
use yii\helpers\Html;
$this->title = "Media Gallery";
$this->params['breadcrumbs'][]= $this->title;

?>
<h1><?php echo Html::encode($this->title)?></h1>


<p>
    <?php echo Html::a('Upload File', ['upload'],['class'=>'btn btn-primary'])?>
    You may change the content of this page by modifying
    the file <code><?= __FILE__; ?></code>.
</p>
<div class="row">
    <?php
    foreach ($medias as $media) {
    ?>
    <div class="col-md-3">
        <div class="card">
            <img src="<?php echo Yii::getAlias('@web').'/'.$media->filepath; ?>" class="card-mg-top" width="100%">
                <div class="card-body">
                    <h5 class="card-title">
                            <?php echo $media->filename;?>
                    </h5>

                </div>

        </div>

    </div>
    <?php
    }
    ?>
</div>

Javob uchun raxmat

Iltimos, saytga kiring yoki ro‘yxatdan o‘ting va shunda ushbu savolga javob berishingiz mumkin bo‘ladi.

Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.

Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin.

Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Telegram kanal YordamUzRss

...