yii框架中文件上传
2016-07-07 10:55
357 查看
1:在数据库中建立一张表(upload,我的表是这样的:
2:使用Gii生成model层;(切记要model层和控制器层一定要有 use yii\web\UploadedFile;控制器一定要调用model哦)
[html] view
plain copy
<?php
namespace app\models;
use yii\web\UploadedFile;
use Yii;
/**
* This is the model class for table "upload".
*
* @property integer $u_id
* @property string $u_name
* @property string $u_pwd
* @property string $u_img
*/
class Upload extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'upload';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['u_name', 'u_pwd', 'u_img'], 'string', 'max' => 255]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'u_id' => 'U ID',
'u_name' => 'U Name',
'u_pwd' => 'U Pwd',
'u_img' => 'U Img',
];
}
public function upload(){
//$this->p_photo->saveAs('uploads/' . $this->p_photo->baseName . '.' . $this->p_photo->extension);
//return true;
return $this->u_img->saveAs('upload/' . $this->u_img->baseName . '.' . $this->u_img->extension);
}
}
3:建立好uploadController.php控制器,在没有post接值的时候,进入views/upload/entry.php,显示视图层,至于views/upload/entry-confirm.php视图,是你接入值后要执行的程序,这里我没有用到这个视图,当接到表单的值之后我又自己生成了个方法;
[html] view
plain copy
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\web\UploadedFile;
use app\models\Upload;
class UploadController extends Controller
{
public function actionIndex(){
$model = new Upload();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// 验证 $model 收到的数据
// 做些有意义的事 ...
return $this->render('entry-confirm', ['model' => $model]);
} else {
// 无论是初始化显示还是数据验证错误
return $this->render('entry', ['model' => $model]);
}
}
public function actionAdds()
{
$model = new Upload();
$request = Yii::$app->request;
//$post = $request->post();
//print_r($data);
$post=$request->post('Upload');
$u_name = $post['u_name'];
$u_pwd = $post['u_pwd'];
//print_r($u_name);
//在浏览器输出的值是 yii\web\UploadedFile Object ( [name] => 2.jpg [tempName] => C:\Windows\php3986.tmp
// [type] => image/jpeg [size] => 216848 [error] => 0 )
$arr = $model->u_img = UploadedFile::getInstance($model,'u_img');
//print_r($arr);
if ($model->upload()) {
// 文件上传成功
$u_img = $arr->name;
//var_dump($u_img);
$connection = \Yii::$app->db;
$result=$connection->createCommand()->insert('upload', [
'u_name' => $u_name,
'u_pwd' => $u_pwd,
'u_img' =>$u_img,
])->execute();
if($result)
{
echo "添加成功";
}
else
{
echo "添加失败";
}
}
}
}
?>
4:视图层显示
[html] view
plain copy
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin([
'action' => ['upload/adds'],
'method'=>'post',
'options' => ['enctype' => 'multipart/form-data']
]); ?>
<?= $form->field($model, 'u_name') ?>
<?= $form->field($model, 'u_pwd') ?>
<?=$form->field($model, 'u_img')->fileInput(['multiple'=>'multiple']);?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
5:至于entry_form.PHP可以有,也可以不要它,因为上边我已经写了actionAdds()这个方法,这个就是当你接值成功列表显示的页面,可以这样写:
[html] view
plain copy
<?php
use yii\helpers\Html;
?>
<p>You have entered the following information:</p>
<ul>
<li><label>Name</label>: <?= Html::encode($model->u_name) ?></li>
<li><label>Email</label>: <?= Html::encode($model->u_pwd) ?></li>
</ul>
好了,就这些了,会有更多方法给大家分享的哦!<img alt="吐舌头" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/tongue.gif" />
2:使用Gii生成model层;(切记要model层和控制器层一定要有 use yii\web\UploadedFile;控制器一定要调用model哦)
[html] view
plain copy
<?php
namespace app\models;
use yii\web\UploadedFile;
use Yii;
/**
* This is the model class for table "upload".
*
* @property integer $u_id
* @property string $u_name
* @property string $u_pwd
* @property string $u_img
*/
class Upload extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'upload';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['u_name', 'u_pwd', 'u_img'], 'string', 'max' => 255]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'u_id' => 'U ID',
'u_name' => 'U Name',
'u_pwd' => 'U Pwd',
'u_img' => 'U Img',
];
}
public function upload(){
//$this->p_photo->saveAs('uploads/' . $this->p_photo->baseName . '.' . $this->p_photo->extension);
//return true;
return $this->u_img->saveAs('upload/' . $this->u_img->baseName . '.' . $this->u_img->extension);
}
}
3:建立好uploadController.php控制器,在没有post接值的时候,进入views/upload/entry.php,显示视图层,至于views/upload/entry-confirm.php视图,是你接入值后要执行的程序,这里我没有用到这个视图,当接到表单的值之后我又自己生成了个方法;
[html] view
plain copy
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\web\UploadedFile;
use app\models\Upload;
class UploadController extends Controller
{
public function actionIndex(){
$model = new Upload();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// 验证 $model 收到的数据
// 做些有意义的事 ...
return $this->render('entry-confirm', ['model' => $model]);
} else {
// 无论是初始化显示还是数据验证错误
return $this->render('entry', ['model' => $model]);
}
}
public function actionAdds()
{
$model = new Upload();
$request = Yii::$app->request;
//$post = $request->post();
//print_r($data);
$post=$request->post('Upload');
$u_name = $post['u_name'];
$u_pwd = $post['u_pwd'];
//print_r($u_name);
//在浏览器输出的值是 yii\web\UploadedFile Object ( [name] => 2.jpg [tempName] => C:\Windows\php3986.tmp
// [type] => image/jpeg [size] => 216848 [error] => 0 )
$arr = $model->u_img = UploadedFile::getInstance($model,'u_img');
//print_r($arr);
if ($model->upload()) {
// 文件上传成功
$u_img = $arr->name;
//var_dump($u_img);
$connection = \Yii::$app->db;
$result=$connection->createCommand()->insert('upload', [
'u_name' => $u_name,
'u_pwd' => $u_pwd,
'u_img' =>$u_img,
])->execute();
if($result)
{
echo "添加成功";
}
else
{
echo "添加失败";
}
}
}
}
?>
4:视图层显示
[html] view
plain copy
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin([
'action' => ['upload/adds'],
'method'=>'post',
'options' => ['enctype' => 'multipart/form-data']
]); ?>
<?= $form->field($model, 'u_name') ?>
<?= $form->field($model, 'u_pwd') ?>
<?=$form->field($model, 'u_img')->fileInput(['multiple'=>'multiple']);?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
5:至于entry_form.PHP可以有,也可以不要它,因为上边我已经写了actionAdds()这个方法,这个就是当你接值成功列表显示的页面,可以这样写:
[html] view
plain copy
<?php
use yii\helpers\Html;
?>
<p>You have entered the following information:</p>
<ul>
<li><label>Name</label>: <?= Html::encode($model->u_name) ?></li>
<li><label>Email</label>: <?= Html::encode($model->u_pwd) ?></li>
</ul>
好了,就这些了,会有更多方法给大家分享的哦!<img alt="吐舌头" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/tongue.gif" />
相关文章推荐
- JHTP自测题_第七章_数组及动态数组(Array and ArrayList)
- 关于ThinkPhp中getField方法存在的问题
- PHP判断二级域名,301跳转
- Thinkphp的致命错误捕获流程
- 织梦后台发布文章提示“标题不能为空”
- thinkphp实现excel数据的导入导出
- 显试调用DLL------GetProcAddress的使用
- PHP:var_dump展示不全
- JHTP小结_第七章_数组及动态数组(Array and ArrayList)
- php自定义函数实现JS的escape的方法示例
- PHP中使用foreach和引用导致程序BUG及解决办法
- @在php中的作用
- PHP使用mysql与mysqli连接Mysql数据库用法示例
- php使用 _before_index() 来实现访问页面前,判断登录
- FTP,SFTP,FTPS区别
- php使用 _before_index() 来实现访问页面前,判断登录
- php使用 _before_index() 来实现访问页面前,判断登录
- Laravel之路由 Route::get/post/any、路由参数、过滤器、命名、子域名、前缀、与模型绑定、抛出 404 错误、控制器
- PHP使用自定义方法实现数组合并示例
- phpCMS 使用总结