单文件上传
模型
[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>