yii2.0單檔案上傳和多檔案上傳

來源:互聯網
上載者:User

標籤:ace   charset   path   view   field   attribute   alert   mime   name   

yii2檔案上傳使用到yii2內建的檔案上傳類UploadFIle,以及對應的模型規則,這裡分別介紹單檔案上傳和多檔案上傳:

 

yii2單個檔案上傳:

上傳步奏,先建立上傳表單模型model(包含驗證規則),其次控制器操作action,以及相對應的view:

model層:

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‘=>‘檔案上傳‘
];
}
}

UploadForm.php  [多檔案上傳模型]

<?php
namespace app\models;

use Yii;
use yii\base\Model;

class UploadForm extends Model
{
/**
* @var UploadedFile|Null file attribute
*/
public $file;

/**
* @return array the validation rules.
*/
public function rules()
{
return [
[[‘file‘], ‘file‘, ‘maxFiles‘ => 10,‘extensions‘=>‘jpg,png,gif‘],
];
}

public function attributeLabels(){
return [
‘file‘=>‘多檔案上傳‘
];
}

Controller層,以TestController中的upload操作和upmore操作

<?php  
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Upload;
use app\models\UploadForm;
use yii\web\UploadedFile;

class TestController extends Controller{

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

/**
* @return string|\yii\web\Response
* 單檔案上傳
*/
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,0775,true);
}
$file->saveAs($path . time() . ‘.‘ . $file->getExtension());
Yii::$app->session->setFlash(‘success‘,‘上傳成功!‘);
return $this->redirect(‘upload‘);
}
}

return $this->render(‘upload‘,[‘model‘=>$model]);
}


public function actionUpmore(){
$model = new UploadForm();
if (Yii::$app->request->isPost) {
$file = UploadedFile::getInstances($model, ‘file‘);

if ($file && $model->validate()) {
echo "<pre/>";

foreach ($file as $fl) {
$fl->saveAs(‘uploads/‘ .mt_rand(1100,9900) .time() .$fl->baseName. ‘.‘ . $fl->extension);
}
Yii::$app->session->setFlash(‘success‘,‘上傳成功!‘);
return $this->redirect(‘upmore‘);
}
}

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

view:層

[單檔案view  uplod.php]

<?php  
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h4>檔案上傳</h4>
<?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>
[多檔案view upmore.php]
<?php  

use yii\helpers\Html;
use yii\widgets\ActiveForm;

?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h4>多檔案上傳</h4>
<?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>  

 

yii2.0單檔案上傳和多檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.