public function index($get) { switch ($get['action']) { case 'config': $result = $this->loadConfig(); $this->output($result, $get['callback']); die; case 'uploadimage': $upload_file = $_FILES['upfile']; if ($upload_file['size'] > Config::get('upload/upload_size')) { $this->output(array("state" => '文件大小超出 ' . Config::get('upload/upload_size') . '字节 限制', "url" => '', "title" => '', "original" => '', "type" => '', "size" => 0), $get['callback']); } $result = Request::postFiles(Config::get('upload/host'), array('ext' => end(explode('.', $upload_file['name']))), array('file' => $upload_file['tmp_name'])); $result = json_decode($result, true); if ($result['code'] == '0') { $url = $result['data']; $file = array_pop(explode('/', $url)); $this->outputHtml(array("state" => 'SUCCESS', "url" => $url, "title" => $file, "original" => $upload_file['name'], "type" => '.' . array_pop(explode('.', $file)), "size" => $upload_file['size']), $get['callback']); } $this->outputHtml(array("state" => $result['message'] ?: '文件上传失败', "size" => $upload_file['size']), $get['callback']); } throw new Exception('action error'); }
public function uploadImage() { if (Router::isPost()) { $file = $_FILES['file']; if (empty($file['name'])) { return new Result('请选择文件', false); } $ext = $this->getFileExt($file['type']); if (!$ext) { return new Result('文件类型不符合,请重新选择文件上传', false); } $rsp = Request::postFiles(Config::get('upload/host'), array(), array('file' => $file['tmp_name'])); $result = json_decode($rsp, true); if ($result['code'] == '0') { return new Result('图片上传成功', null, array('src' => Config::get('upload/url') . $result['data'], 'value' => $result['data'])); } return new Result('图片上传失败,请稍候重试', null, $rsp); } return array('UPLOAD_PAGE_URL' => Router::getUrl('index/uploadImage')); }