/** * 上传文件处理 * @param HttpRequest $request */ public function upload(HttpRequest $request) { $config = array("upload_dir" => RES_PATH . "upload/" . date('Y') . "/" . date('m'), 'allow_ext' => 'jpg|jpeg|png|gif|txt|pdf|rar|zip|swf|bmp|c|java|mp3', 'max_width' => 0, 'max_height' => 0, 'max_size' => 1024000); $upload = new FileUpload($config); $result = $upload->upload('src'); __print($result); __print($upload->getUploadMessage()); die; }
/** * 上传文件,返回上传文件的地址 * @param HttpRequest $request */ public function index(HttpRequest $request) { $userid = $request->getParameter('userid', 'intval'); $mediaId = $request->getParameter('media_id', 'intval'); $type = $request->getParameter('type', 'trim'); //图片的最大尺寸,大于这个尺寸就等比例裁剪 $width = $request->getParameter('width', 'intval'); if ($width <= 0) { $width = 800; } if (!$type) { $type = 'image'; } $__uploadDir = $type . '/' . date('Y') . '/' . date('m') . '/' . date('d'); $uploadDir = getConfig('upload_dir') . $__uploadDir; //上传图片 $config = array("upload_dir" => $uploadDir, 'max_size' => 2048000); $upload = new FileUpload($config); $result = $upload->upload('Filedata'); if ($result && $result['is_image'] == 1) { //缩放图片 if ($width < $result['image_width']) { $src = $result['file_path']; $thumb = ImageThumb::getInstance(); $thumb->setFlag(2); //规定宽度,等比缩放 $filename = $thumb->makeThumb(array($width, 0), $src, null, true); //重新获取图片的尺寸和大小 $info = getimagesize($filename); $result['image_width'] = $info[0]; $result['image_height'] = $info[1]; $result['file_size'] = filesize($filename); } //添加图片图片信息到数据库 $service = Beans::get('image.image.service'); $url = '/res/upload/' . $__uploadDir . '/' . $result['file_name']; $data = array('userid' => $userid, 'media_id' => $mediaId, 'url' => $url, 'filename' => $result['local_name'], 'type' => $type, 'filesize' => formatFileSize($result['file_size']), 'width' => intval($result['image_width']), 'height' => intval($result['image_height']), 'add_time' => time(), 'grabed' => 1); //保存到数据库失败 if (!$service->add($data)) { //删除图片 @unlink($result['file_path']); } AjaxResult::ajaxResult(1, $url); } else { @unlink($result['file_path']); AjaxResult::ajaxResult(0, '图片上传失败,' . $upload->getUploadMessage()); } }
/** * 编辑器的上传文件,图片,涂鸦 * @param $configs * @return mixed */ public function ueUpload($configs) { //文件上传配置 $uploadConfigs = array(); //上传文件的目录 $dir = 'image'; //上传表单name $fieldName = $configs['imageFieldName']; $base64 = false; if ($configs['action'] == 'uploadimage') { $uploadConfigs['allow_ext'] = $configs['imageAllowFiles']; $uploadConfigs['max_size'] = $configs['imageMaxSize']; $uploadConfigs['max_width'] = $configs['imageMaxWidth']; $dir = $configs['imagePathFormat']; } elseif ($configs['action'] == 'uploadscrawl') { //$uploadConfigs['allow_ext'] = $configs['scrawlAllowFiles']; $uploadConfigs['max_size'] = $configs['scrawlMaxSize']; $fieldName = $configs['scrawlFieldName']; $dir = $configs['scrawlPathFormat']; $base64 = true; } elseif ($configs['action'] == 'uploadfile') { $uploadConfigs['allow_ext'] = $configs['fileAllowFiles']; $uploadConfigs['max_width'] = $configs['fileMaxWidth']; $uploadConfigs['max_size'] = $configs['fileMaxSize']; $dir = $configs['filePathFormat']; $fieldName = $configs['fileFieldName']; } $_dir = $dir . '/' . date('Y') . '/' . date('m') . '/' . date('d'); $uploadDir = getConfig('upload_dir') . $_dir; $uploadConfigs['upload_dir'] = $uploadDir; //将允许文件类型处理成FileUpload类所接受的形式 foreach ($uploadConfigs['allow_ext'] as $key => $values) { $uploadConfigs['allow_ext'][$key] = ltrim($values, '.'); } $uploadConfigs['allow_ext'] = implode('|', $uploadConfigs['allow_ext']); $uploadConfigs['max_size'] = 2048000; $fileUpload = new FileUpload($uploadConfigs); $fileInfo = $fileUpload->upload($fieldName, $base64); /** * 得到上传文件所对应的各个参数,数组结构 * array( * "state" => "", //上传状态,上传成功时必须返回"SUCCESS" * "url" => "", //返回的地址 * "title" => "", //新文件名 * "original" => "", //原始文件名 * "type" => "" //文件类型 * "size" => "", //文件大小 * ) */ $result = array(); //上传失败 if ($fileInfo == false) { $result['state'] = $fileUpload->getUploadMessage(); //上传成功 } else { $url = '/res/upload/' . $_dir . '/' . $fileInfo['file_name']; $result['state'] = 'SUCCESS'; $result['url'] = $url; $result['title'] = $fileInfo['raw_name']; $result['original'] = $fileInfo['local_name']; $result['original'] = $fileInfo['local_name']; $result['type'] = '.' . $fileInfo['file_ext']; $result['size'] = $fileInfo['file_size']; //插入数据 $service = Beans::get('image.image.service'); $mediaService = Beans::get('media.media.service'); $loginMedia = $mediaService->getLoginMedia(); $type = $dir; //涂鸦也是图片 if ($type == 'scraw') { $type = 'image'; } $userid = intval($this->loginUser['id']); $mediaId = intval($loginMedia['id']); //如果是通过后台修改用户的数据,则将注前台册用户的id if (!$userid) { Session::start(); $userid = intval($_SESSION['front_userid']); } if (!$mediaId) { Session::start(); $mediaId = intval($_SESSION['front_mediaId']); } $data = array('userid' => $userid, 'media_id' => $mediaId, 'url' => $url, 'filename' => $fileInfo['local_name'], 'type' => $type, 'filesize' => formatFileSize($fileInfo['file_size']), 'width' => $fileInfo['image_width'], 'height' => $fileInfo['image_height'], 'add_time' => time(), 'grabed' => 1); //保存到数据库失败 if (!$service->add($data)) { //删除图片 @unlink($fileInfo['file_path']); $result['state'] = '保存数据失败!'; } } /* 返回数据 */ return json_encode($result); }