YII2单文件上传,YII2多文件上传,YII2批量上传

5 篇文章 0 订阅

单文件上传

模型

[Upload.php]

<?php
namespace app\models;
use Yii;
use yii\base\Model;

class Upload extends Model{
    public $file;
    public function rules(){
        return [
            [['file'], 'file', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png',],
        ];
    }
    public function attributeLabels(){
        return [
            'file'=>'文件上传'
        ];
    }
} 

控制器

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\Upload;
use yii\web\UploadedFile;

class TestController extends  Controller{

    public function actionIndex(){
        return  $this->renderPartial('index');
    }
    public function actionUpload(){
        $model= new Upload();

        if (Yii::$app->request->isPost) {
            $file = UploadedFile::getInstance($model, 'file');
            $path='uploads/'.date("YmdH",time()).'/';
            if ($file && $model->validate()) {
                if(!file_exists($path)){
                    mkdir($path,0777);
                }
                $file->saveAs($path . time() . '.' . $file->getExtension());
                Yii::$app->session->setFlash('success','上传成功!');
                return $this->redirect('upload');
            }
        }
        return $this->render('upload',['model'=>$model]);
    }

视图

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
<?php if(Yii::$app->session->hasFlash('success')):?>
    <div class="alert alert-danger">
    <?=Yii::$app->session->getFlash('success')?>
    </div>
<?php endif ?>
<?php $form=ActiveForm::begin([
    'id'=>'upload',
    'enableAjaxValidation' => false,
    'options'=>['enctype'=>'multipart/form-data']
]);
?>
<?= $form->field($model, 'file')->fileInput();?>
<?=  Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?php ActiveForm::end(); ?>

</body>
</html>

多文件上传

模型

[Uploadmore.php]

<?php
namespace app\models;
use Yii;
use yii\base\Model;

class UploadForm extends Model
{

    public $file;
    public function rules()
    {
        return [
            [['file'], 'file', 'maxFiles' => 10,'extensions'=>'jpg,png,gif'],
        ];
    }

    public function attributeLabels(){
        return [
            'file'=>'多文件上传'
        ];
    }
}

控制器

namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\Uploadmore;
use yii\web\UploadedFile;

class TestController extends  Controller{

    public function actionIndex(){
        return  $this->renderPartial('index');
    }



    public function actionUpmore(){
        $model = new UploadForm();
        if (Yii::$app->request->isPost) {
            $file = UploadedFile::getInstances($model, 'file');
        $path='uploads/'.date("YmdH",time()).'/';
            if ($file && $model->validate()) {
                if(!file_exists($path)){
                    mkdir($path,0777);
                }
                foreach ($file as $key=> $fl) {
                    $fl->saveAs('uploads/' .$key.time() .$fl->baseName. '.' . $fl->extension);
                }
                Yii::$app->session->setFlash('success','上传成功!');
                return $this->redirect('upmore');
            }
        }

        return $this->render('upmore', ['model' => $model]);
    }
} 

视图

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>多文件上传</title>
</head>
<body>
<?php if(Yii::$app->session->hasFlash('success')):?>
    <div class="alert alert-danger">
        <?=Yii::$app->session->getFlash('success')?>
    </div>
<?php endif ?>
<?php $form=ActiveForm::begin([
    'id'=>'upload',
    'enableAjaxValidation' => false,
    'options'=>['enctype'=>'multipart/form-data']
]);
?>
<?= $form->field($model, 'file[]')->fileInput(['multiple' => true]);?>
<?=  Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?php ActiveForm::end(); ?>

</body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值