private function fileImageUpload($option)
 {
     if (empty($option['width'])) {
         $option['width'] = 600;
     }
     if (!empty($_FILES['file']['name'])) {
         $ret = Utility::upload($_FILES['file']);
         if (is_error($ret)) {
             $this->frameCallback($ret);
         }
         File::imageThumb($ret['abs'], $ret['abs'], $option['width']);
         $result = array();
         $result['filename'] = $ret['filename'];
         $result['url'] = $ret['url'];
         $result['error'] = 0;
         $this->frameCallback($result);
     } else {
         $this->frameCallback(error(-1, '请选择要上传的图片!'));
     }
 }
 public function bomAction()
 {
     if (IS_POST) {
         $f = I('post.file');
         if (!empty($f)) {
             $fname = MB_ROOT . $f;
             if (is_file($fname)) {
                 $data = file_get_contents($fname);
                 $bom = substr($data, 0, 3);
                 if ($bom == "") {
                     $data = substr($data, 3);
                     file_put_contents($fname, $data);
                     exit('success');
                 }
             }
             exit;
         }
         $path = MB_ROOT;
         $tree = File::tree($path);
         $ds = array();
         foreach ($tree as $t) {
             $t = str_replace($path, '', $t);
             $t = str_replace('\\', '/', $t);
             if (preg_match('/^.*\\.php$/', $t)) {
                 $fname = $path . $t;
                 $fp = fopen($fname, 'r');
                 if (!empty($fp)) {
                     $bom = fread($fp, 3);
                     fclose($fp);
                     if ($bom == "") {
                         $ds[] = $t;
                     }
                 }
             }
         }
         $this->assign('ds', $ds);
     }
     C('FRAME_CURRENT', U('control/site/flush'));
     $this->display();
 }
 public function addonsAction()
 {
     $addons = array();
     $path = MB_ROOT . 'addons/';
     if (is_dir($path)) {
         if ($handle = opendir($path)) {
             while (false !== ($addonpath = readdir($handle))) {
                 if ($addonpath != '.' && $addonpath != '..') {
                     $define = Addon::getAddon($addonpath, true);
                     if (!is_error($define)) {
                         $addons[] = $define;
                     }
                 }
             }
         }
     }
     if (IS_POST) {
         $a = I('post.addon');
         $addons = coll_key($addons, 'name');
         $addon = $addons[$a];
         if (!empty($addon)) {
             $zip = new \ZipArchive();
             $tmpFile = ADDON_CURRENT_PATH . 'Data/package.zip';
             @unlink($tmpFile);
             $zip->open($tmpFile, \ZipArchive::CREATE);
             $root = MB_ROOT . "addons/{$a}";
             $files = File::tree($root);
             foreach ($files as $file) {
                 $local = substr($file, strlen($root));
                 if (substr($local, -4) == '.php') {
                     if (I('post.trim') != '') {
                         $content = $this->trimComments($file, I('post.license'));
                     }
                     $zip->addFromString("{$a}{$local}", $content);
                 } else {
                     $zip->addFile($file, "{$a}{$local}");
                 }
             }
             $zip->close();
             $version = MB_VERSION;
             $filename = "{$a}-v{$addon['version']} (for MB-{$version})";
             header('content-type: application/zip');
             header('content-disposition: attachment; filename="' . $filename . '.zip"');
             readfile($tmpFile);
             @unlink($tmpFile);
         }
     }
     $this->assign('addons', $addons);
     $this->display('addons');
 }
Esempio n. 4
0
 /**
  * 上传文件保存,缩略图暂未实现
  *
  * @param string $file  上传的$_FILE字段
  * @param string $type  上传类型(将按分类保存不同子目录,image -> images)
  * @param string $sname 保存的文件名,如果为 auto 则自动生成文件名,否则请指定从附件目录开始的完整相对路径(包括文件名,不包括文件扩展名)
  * @param array $extra
  * @return array 返回结果数组,字段包括:success => bool 是否上传成功,path => 保存路径(从附件目录开始的完整相对路径)
  */
 public static function upload($file, $type = 'image', $sname = 'auto')
 {
     if (empty($file)) {
         return error(-1, '没有上传内容');
     }
     $type = in_array($type, array('image', 'audio')) ? $type : 'image';
     $settings = array('image' => array('storage' => 'images/', 'extentions' => array('jpg', 'png'), 'limit' => 1024));
     if (!array_key_exists($type, $settings)) {
         return error(-1, '未知的上传类型');
     }
     $extention = pathinfo($file['name'], PATHINFO_EXTENSION);
     if (!in_array(strtolower($extention), $settings[$type]['extentions'])) {
         return error(-1, '不允许上传此类文件');
     }
     if (!empty($settings[$type]['limit']) && $settings[$type]['limit'] * 1024 < filesize($file['tmp_name'])) {
         return error(-1, "上传的文件超过大小限制,请上传小于 {$settings[$type]['limit']}k 的文件");
     }
     $path = MB_ROOT . '/attachment/';
     $ret = array();
     if ($sname == 'auto') {
         $ret['filename'] = $settings[$type]['storage'] . date('Y/m/');
         File::mkdirs($path . $ret['filename']);
         do {
             $filename = util_random(30) . ".{$extention}";
         } while (file_exists($path . $ret['filename'] . $filename));
         $ret['filename'] .= $filename;
     } else {
         $ret['filename'] = $settings[$type]['storage'] . $sname;
         mkdirs(dirname($path . $ret['filename']));
     }
     $ret['abs'] = $path . $ret['filename'];
     if (!File::move($file['tmp_name'], $ret['abs'])) {
         return error(-1, '保存上传文件失败');
     }
     $ret['url'] = attach('attachment/' . $ret['filename']);
     return $ret;
 }