示例#1
0
/**
 * 文件上传
 * @param  string $save_path 保存路径
 * @return array
 */
function upload($save_path, $size = -1, $rule = 'uniqid')
{
    $upload = new \Org\Util\UploadFile();
    // 文件大小
    $upload->maxSize = $size;
    //设置附件上传目录
    $upload->savePath = WEB_ROOT . $save_path;
    // 上传文件名唯一
    $upload->saveRule = $rule;
    if (!$upload->upload()) {
        //捕获上传异常
        return array('status' => false, 'info' => $upload->getErrorMsg());
    }
    // 得到上传的文件路径
    $info = $upload->getUploadFileInfo();
    foreach ($info as $key => $item) {
        $info[$key]['path'] = $save_path . $item['savename'];
    }
    return array('status' => true, 'info' => $info);
}
 public function checkPic()
 {
     // 取得时间戳
     $date = date('Y', time());
     $upload = new \Org\Util\UploadFile();
     $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
     // 设置附件上传类型
     $upload->autoSub = true;
     $upload->subType = 'date';
     $upload->dateFormat = 'ym';
     $upload->savePath = '/Uploads/img/' . $date . '/';
     // 设置附件上传目录
     if ($upload->upload()) {
         $info = $upload->getUploadFileInfo();
         echo json_encode(array('url' => $info[0]['savename'], 'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $info[0]['name'], 'state' => 'SUCCESS'));
     } else {
         echo json_encode(array('state' => $upload->getErrorMsg()));
     }
 }