You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
simple-yewu/frontend/controllers/FileUploadController.php

38 lines
974 B

6 years ago
<?php
/**
* Created by PhpStorm.
* User: liuyingjie
* Date: 2017/9/18
* Time: 9:16
*/
namespace frontend\controllers;
use common\models\FixCarImg;
use Yii;
use yii\helpers\FileHelper;
use yii\web\Controller;
use yii\web\UploadedFile;
class FileUploadController extends Controller
{
public function actionUpload(){
$request = Yii::$app->request;
if($request->isPost){
$type = $request->post('category');
$id = $request->post('fix_id');
$img = UploadedFile::getInstanceByName('file');
$filename = 'upload/images/'.date('Ym').'/'.time().rand(1111,9999).'.'.$img->extension;
FileHelper::createDirectory(dirname($filename));
$img->saveAs($filename);
$fix_car_img = new FixCarImg();
$fix_car_img->name = $filename;
$fix_car_img->fix_id = $id;
$fix_car_img->type = $type;
return $fix_car_img->save();
}
}
}