一个文件上传案例,就是上传不了 [ 2.0 版本 ]
view层
$form = ActiveForm::begin(["options" => ["enctype" => "multipart/form-data"]]); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'grade_id')->dropDownList(
Grade::find()
->select(['grade','id'])
->orderBy('id')
->indexBy('id')
->column(),
['prompt'=>'请选择班级']
) ;?>
<?= $form->field($model, 'path')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton('上传', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
Controller层
public function actionCreate()
{
$model = new Video();
if (Yii::$app->request->isPost )
{
$model->author_id = \Yii::$app->user->identity->id ;
$model->create_time = date('Y-m-d');
$model->status = 0;
$pathName = UploadedFile::getInstance($model, "path");
var_dump($pathName);
//文件名
$fileName = date("HiiHsHis").mt_rand(1100,9900). "." . $pathName->extension;
//文件上传存放的目录
$dirPath = "../../upload/videos/".date("Ymd");
$dirPathDb = "=upload/videos/".date("Ymd");
// var_dump($fileName);
// var_dump($dirPath);
if (!is_dir($dirPath))
mkdir($dirPath);
//文件目录+文件名
$dir = $dirPathDb."/". $fileName;
// var_dump($dir);
//保存到数据库
//$model->path=$dir;
//添加入库
// $res = Yii::$app->db->createCommand()
// ->insert('video',['path'=>$dir,'author_id'=>\Yii::$app->user->identity->id,'create_time'=>date('Y-m-d'),
// 'status'=>0,'title'=>$model->title,'tags'=>$model->tags,'grade_id'=>$model->grade_id,'subject_id'=>$model->subject_id,
// 'img_up'=>$model->img_up
// ])
// ->execute();
//echo 5;
// if($pathName->saveAs($dir) === true){
// $model->path=$dir;
// echo 3;
// }
$pathName->saveAs($dirPath,true);
$model->path = $dirPathDb."/". $fileName;
if ($model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
else {
return $this->render('create', [
'model' => $model,]);
}
}
else {
echo 2;
return $this->render('create', [
'model' => $model,
]);
}
}
可是就是上传不上去
测试了很久
私念 补充于 2018-05-20 17:19
Controller层
view层
私念 补充于 2018-05-21 13:21
model层
共 4 个回答
-
// 结果 object(yii\web\UploadedFile)#223 (5) { ["name"]=> string(22) "蘑菇头跳舞啦.mp4" ["tempName"]=> string(14) "/tmp/phpshHSiG" ["type"]=> string(9) "video/mp4" ["size"]=> int(1224988) ["error"]=> int(0) }
打印
public function actionCreate() { $model = new Unit(); $picurl = UploadedFile::getInstance($model, 'pic'); echo '<pre>'; var_dump($picurl);die; if ($model->load(Yii::$app->request->post()) && $model->save()) { // return $this->redirect(['view', 'id' => $model->id]); $this->doLog('创建单位', '单位ID' . $model->id); return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } }
模型
['pic', 'file', 'checkExtensionByMimeType' => false, 'extensions' => 'mp4, flv', 'skipOnEmpty' => false],
视图
<?= $form->field($model, 'pic')->fileInput() ?>
没有发现错误啊
共 1 条回复私念 觉得很赞
私念
注册时间:2017-07-03
最后登录:2019-10-09
在线时长:4小时2分
最后登录:2019-10-09
在线时长:4小时2分
- 粉丝0
- 金钱64
- 威望0
- 积分104