/**
*用户文件上传
*/
public function userFile($file='', $exts=[]){
$config = array(
'maxSize' =>'3145728',// 设置附件上传大小
'savePath' => './Users/',//设置保存路径
'exts' =>$exts,// 设置附件上传类型
'autoSub'=> true,//自动使用子目录保存上传文件 默认为true
'subName' => array('date', 'Ymd'),//子目录创建方式,采用数组或者字符串方式定义
);
$upload = new \Think\Upload($config);//实例化上传类
if(!$info = $upload->upload($file)){
//if there isn't a file will send a error message.
$res = [
'status' => 0,
'message'=>$upload->getError()
];
$this->ajaxReturn($res);
}
$url=[];//it is a path to save the files
foreach ($info as $k => $file){
$url[$k] = str_ireplace('./','/Uploads/',$file['savepath'].$file['savename']);
}
$res = [
'status' => 1,
'url' =>$url
];
return $res;
}
多表联合
Public function lst(){
$news = M('News');
$new = $news->limit(I('get.offset'),I('get.limit'))->select();
$total = count($news->select());
$n_type = M('news_type');
foreach ($new as $key => $v) {
$temp1 = $n_type->find($v['type']);
$new[$key]['type'] = $temp1['name'];
}
$data=[
'total' => $total,
'rows' =>$new
];
//print_r($data1);exit();
$this->ajaxReturn($data);
}
后台进行修改操作时,没有修改图片,再次刷新图片地址为空?
需要在修改函数中加入一个去除空字符串的函数.
/**
* 去除数组中的空字符串
*/
function removeEmpty($arr) {
$array = $arr;
foreach ($array as $key => $v) {
if (trim($v) == '') {
unset($array[$key]);
}
}
return $array;
}
网友评论