//图片上传处理类(这里略)
//upload.php事例代码
namespace backend\components;
class Upload
{
public function test(){
$a = '你好';
return $a;
}
}
'components' => [
'imgload' => [
'class' => 'backend\components\Upload'
],
]
public function actionArticlelist()
{
$cc = Yii::$app->imgload->test();
var_dump($cc);exit;
}
'components' => [
'user' => [
'identityClass' => 'common\models\AgMerch',
'enableAutoLogin' => true,
],
//自定义图片上传类
'imgload' => [
'class' => 'agent\components\Upload'
],
]
namespace agent\components;
use yii;
use yii\base\Object;
use yii\web\UploadedFile;
class Upload extends Object
{
/**
* [UploadPhoto description]
* @param [type] $model [实例化模型]
* @param [type] $path [图片存储路径]
* @param [type] $originName [图片源名称]
* @param boolean $isthumb [是否要缩略图]
*/
public function UploadPhoto($model,$path,$originName,$isthumb=false){
$root = $_SERVER['DOCUMENT_ROOT'].'/'.$path;
//返回一个实例化对象
$files = UploadedFile::getInstance($model,$originName);
$folder = date('Ymd')."/";
$pre = rand(999,9999).time();
if($files && ($files->type == "image/jpeg" || $files->type == "image/pjpeg" || $files->type == "image/png" || $files->type == "image/x-png" || $files->type == "image/gif"))
{
$newName = $pre.'.'.$files->getExtension();
}else{
die($files->type);
}
if($files->size > 2000000){
die("上传的文件太大");
}
if(!is_dir($root.$folder))
{
if(!mkdir($root.$folder, 0777, true)){
die('创建目录失败...');
}else{
// chmod($root.$folder,0777);
}
}
//echo $root.$folder.$newName;exit;
if($files->saveAs($root.$folder.$newName))
{
if($isthumb){
$this->thumbphoto($files,$path.$folder.$newName,$path.$folder.'thumb'.$newName);
return $path.$folder.$newName.'#'.$path.$folder.'thumb'.$newName;
}else{
return $path.$folder.$newName;
}
}
}
}
use backend\components\Upload;
public function actionArtadd(){
$model = new Article();
if ($model->load(Yii::$app->request->post())) {
//cover为表中的字段名
$img = Yii::$app->imgload->UploadPhoto($model,'uploads/article/','cover');
$model->cover = $img;//存入表中
if($model->save()){
Yii::$app->getSession()->setFlash('info', '添加成功!');
return $this->redirect(['xxx']);
}else{
Yii::$app->getSession()->setFlash('info', '添加失败!');
@unlink($img);
return $this->redirect(['xxx']);
}
}
//代码略
}
<?= $form->field($model, 'cover', [ 'options'=>['class'=>''], 'inputOptions' => ['class' => 'form-control'], ])->fileInput()->label(false); ?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有