<?php
控制器部分
class TblAdminController extends Controller{
public $layout = '//layouts/column';
public function actionIndex(){
$data = array("admin"=>'admin1','password'=>'password1');
$this->render('index',$data);
}
public function actionView(){
$this->render('view');
}
public function actionUploadimg(){
$isSuc = false;
$root = YiiBase::getPathOfAlias('webroot');
$folder = $root.'/images/';
$this->mkDirIfNotExist($folder);
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg"))) //&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0){
$isSuc = false;
}else{
$tmpFilePath = $_FILES["file"]["tmp_name"];
$desFilePath = $folder.$_FILES["file"]["name"];
if (file_exists($desFilePath)){
//unlink($desFilePath);
echo $_FILES["file"]["name"] . " already exists. ";
exit;
}else{
move_uploaded_file($tmpFilePath, $desFilePath);
$isSuc = true;
}
}
}else{
echo "Invalid file";exit;
}
}
function mkDirIfNotExist($dir){
if(!is_dir($dir)){
if(!mkdir($dir, 0, true)){
throw new Exception('create folder fail');
//return false;
}else{
return true;
}
}
return false;
}
}
//视图部分
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form action="index.php?r=tbladmin/uploadimg" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
Yii内部的文件上传
最新推荐文章于 2021-04-12 14:09:11 发布