function upload($source, $target) { $obj = new ALIOSS(); $obj->set_host_name($this->hostname, $this->port); $obj->set_debug_mode(FALSE); $bucket = $this->bucket; $response = $obj->upload_file_by_file($bucket, $target, $source); $rt = jishigou_oss::status($response); return $rt == '2' ? 1 : 0; }
/** * 上传文件到oss * @param string $localPath * @param string $ossPath 在oss上的路径: * 朋友圈 md5(member_id)/date('Y').date('m').date('d').'/'.pic.png * 个人资料 md5(member_id)/pic.png * * */ public static function upload($localPath, $ossPath) { $oss_sdk_service = new \ALIOSS(); $oss_sdk_service->set_debug_mode(true); $bucket = BUCKET; $object = $ossPath; $file_path = $localPath; $response = $oss_sdk_service->upload_file_by_file($bucket, $object, $file_path); $header = $response->header; return $header['_info']; }
public function aliyuntest() { $id = $this->input->get('id'); $host = $this->input->get('host'); $rurl = $this->input->get('rurl'); $secret = $this->input->get('secret'); $bucket = $this->input->get('bucket'); if (!$id || !$host || !$secret || !$bucket) { exit(lang('035')); } if (!$rurl) { exit(lang('199')); } require_once FCPATH . 'omooo/libraries/AliyunOSS/sdk.class.php'; $oss = new ALIOSS($id, $secret, $host); $response = $oss->upload_file_by_file($bucket, 'test.txt', FCPATH . 'index.php'); if ($response->status == 200) { $oss->delete_object($bucket, 'test.txt'); if (strpos(dr_catcher_data($rurl . '/test.txt'), 'omooo.com') === FALSE) { exit(lang('200')); } exit('ok'); } else { exit($response->body); } }
/** * 上传 * * @param intval $uid uid 用户id * @param array $info ci 文件上传成功返回数据 * @return array */ public function upload($uid, $info) { $_ext = strtolower(substr($info['file_ext'], 1)); $author = $this->_get_member_name($uid); $content = file_get_contents($info['full_path']); // 入库附件 $this->db->replace('attachment', array('uid' => $uid, 'author' => $author, 'siteid' => $this->siteid, 'tableid' => 0, 'related' => '', 'fileext' => $_ext, 'filemd5' => $content ? md5($content) : 0, 'download' => 0, 'filesize' => $info['file_size'] * 1024)); $id = $this->db->insert_id(); unset($content); // 入库失败,返回错误且删除附件 if (!$id) { @unlink($info['full_path']); return lang('m-145'); } $remote = 0; $attachment = $file = substr($info['full_path'], strlen(FCPATH)); // 附件配置信息 $config = $this->ci->get_cache('siteinfo', SITE_ID, 'remote', SITE_ATTACH_REMOTE); // 远程附件模式 if ($config && $config['SITE_ATTACH_EXTS'] && ($_exts = explode(',', $config['SITE_ATTACH_EXTS'])) && in_array($_ext, $_exts)) { // 根据模式来存储 set_time_limit(0); if (SITE_ATTACH_REMOTE == 1) { // ftp附件模式 $this->load->library('ftp'); if ($this->ftp->connect(array('port' => $config['SITE_ATTACH_PORT'], 'debug' => FALSE, 'passive' => $config['SITE_ATTACH_PASV'], 'hostname' => $config['SITE_ATTACH_HOST'], 'username' => $config['SITE_ATTACH_USERNAME'], 'password' => $config['SITE_ATTACH_PASSWORD']))) { // 连接ftp成功 $dir = basename(dirname($info['full_path'])) . '/'; $file = basename($info['full_path']); $path = $config['SITE_ATTACH_PATH'] . '/' . $dir; $this->ftp->mkdir($path); if ($this->ftp->upload($info['full_path'], $path . $file, $config['SITE_ATTACH_MODE'], 0775)) { $remote = 1; $attachment = $dir . $file; $file = $config['SITE_ATTACH_URL'] . '/' . $attachment; unlink($info['full_path']); } $this->ftp->close(); } else { log_message('error', '远程附件ftp模式:ftp连接失败'); } } elseif (SITE_ATTACH_REMOTE == 2) { // 百度云存储模式 $file = basename(dirname($info['full_path'])) . '/' . basename($info['full_path']); require_once FCPATH . 'omooo/libraries/BaiduBCS/bcs.class.php'; $bcs = new BaiduBCS($config['ak'], $config['sk'], $config['host']); $opt = array(); $opt['acl'] = BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_WRITE; $opt['curlopts'] = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 1800); $response = $bcs->create_object($config['bucket'], '/' . $file, $info['full_path'], $opt); if ($response->status == 200) { $remote = 2; $attachment = $file; $file = $config['SITE_ATTACH_URL'] . '/' . $attachment; unlink($info['full_path']); } else { log_message('error', '远程附件百度云存储失败'); } } elseif (SITE_ATTACH_REMOTE == 3) { // 阿里云存储模式 $file = basename(dirname($info['full_path'])) . '/' . basename($info['full_path']); require_once FCPATH . 'omooo/libraries/AliyunOSS/sdk.class.php'; $oss = new ALIOSS($config['id'], $config['secret'], $config['host']); $oss->set_debug_mode(FALSE); $response = $oss->upload_file_by_file($config['bucket'], $file, $info['full_path']); if ($response->status == 200) { $remote = 3; $attachment = $file; $file = $config['SITE_ATTACH_URL'] . '/' . $attachment; unlink($info['full_path']); } else { log_message('error', '远程附件阿里云存储模式:' . $response->body); } } } // 非远程附件补全本地地址 if (!$remote) { $file = SITE_URL . $file; } $pos = strrpos($info['client_name'], '.'); $filename = strpos($info['client_name'], 'http://') === 0 ? trim(strrchr($info['client_name'], '/'), '/') : $info['client_name']; $filename = $pos ? substr($filename, 0, $pos) : $filename; // 增加至未使用附件表 $this->db->replace('attachment_unused', array('id' => $id, 'uid' => $uid, 'author' => $author, 'siteid' => $this->siteid, 'remote' => $remote, 'fileext' => $_ext, 'filename' => $filename, 'filesize' => $info['file_size'] * 1024, 'inputtime' => SYS_TIME, 'attachment' => $attachment, 'attachinfo' => '')); return array($id, $file, $_ext); }
function syn_to_remote_file_server($url) { if ($GLOBALS['distribution_cfg']['OSS_TYPE'] && $GLOBALS['distribution_cfg']['OSS_TYPE'] != "NONE") { if ($GLOBALS['distribution_cfg']['OSS_TYPE'] == "ES_FILE") { $pathinfo = pathinfo($url); $file = $pathinfo['basename']; $dir = $pathinfo['dirname']; $dir = str_replace("public/", "", $dir); $filefull = SITE_DOMAIN . APP_ROOT . "/public/" . $dir . "/" . $file; $syn_url = $GLOBALS['distribution_cfg']['OSS_DOMAIN'] . "/es_file.php?username="******"&password="******"&file=" . $filefull . "&path=" . $dir . "/&name=" . $file . "&act=0"; @file_get_contents($syn_url); } elseif ($GLOBALS['distribution_cfg']['OSS_TYPE'] == "ALI_OSS") { $pathinfo = pathinfo($url); $file = $pathinfo['basename']; $dir = $pathinfo['dirname']; $ali_oss_sdk = APP_ROOT_PATH . "system/alioss/sdk.class.php"; if (file_exists($ali_oss_sdk)) { require_once $ali_oss_sdk; if (class_exists("ALIOSS")) { $oss_sdk_service = new ALIOSS(); $oss_sdk_service->set_debug_mode(FALSE); $bucket = $GLOBALS['distribution_cfg']['OSS_BUCKET_NAME']; $object = $dir . "/" . $file; $file_path = APP_ROOT_PATH . $dir . "/" . $file; $oss_sdk_service->upload_file_by_file($bucket, $object, $file_path); } } } } }
/** *上传函数 *@param $object *@param $file *@param $opt *@return bool */ function _file_upload($object, $file, $opt = array()) { //设置超时时间 //set_time_limit(120); //如果文件不存在,直接返回FALSE if (!@file_exists($file)) { return FALSE; } //获取WP配置信息 $oss_options = get_option('oss_options', TRUE); $oss_bucket = attribute_escape($oss_options['bucket']); $oss_ak = attribute_escape($oss_options['ak']); $oss_sk = attribute_escape($oss_options['sk']); $oss_host = attribute_escape($oss_options['host']); if ($oss_host == null || $oss_host == '') { $oss_host = 'oss.aliyuncs.com'; } if (@file_exists($file)) { try { //实例化存储对象 if (!is_object($aliyun_oss)) { $aliyun_oss = new ALIOSS($oss_ak, $oss_sk, $oss_host); } //上传原始文件,$opt暂时没有使用 $aliyun_oss->upload_file_by_file($oss_bucket, $object, $file, $opt); return TRUE; } catch (Exception $ex) { return FALSE; } } else { return FALSE; } }
// define('M_PATH',$floder); //设置监控的目录,当前目录为'.',上一级目录为'..',也可以设置绝对路径,后面不要加斜杠 define('M_LOG', './m.log'); //设置存储log的路径,可以放置在任意位置 define('M_FILE', './f.log'); //待上传的文件列表 $filesarr = array(); //待更新文件 if (file_exists(M_FILE)) { _log('m file is exists.'); $filesarr = unserialize(file_get_contents(M_FILE)); foreach ($filesarr as $key => $file) { _log('m file :' . $file); //上传动作: $object = str_replace($host, "", $file); $file_path = $file; $response = $oss_sdk_service->upload_file_by_file($bucket, $object, $file_path); _format($response); unset($filesarr[$key]); file_put_contents(M_FILE, serialize($filesarr)); } @unlink(M_FILE); exit; } $file_list = array(); foreach ($floder as $f) { record_md5($f); } if (file_exists(M_LOG)) { $log = unserialize(file_get_contents(M_LOG)); } else { $log = array();
function oss_upload($src, $object, $dir) { include_once PHPDISK_ROOT . 'includes/oss/sdk.class.php'; $obj = new ALIOSS(); $obj->set_debug_mode(false); $bucket = get_oss_info('oss_bucket'); //$object = 'netbeans-7.1.2-ml-cpp-linux.sh'; //$file_path = "D:\\TDDOWNLOAD\\netbeans-7.1.2-ml-cpp-linux.sh"; $response = $obj->upload_file_by_file($bucket, get_oss_info('id') . '/' . $dir . $object, $src); //_format($response); return $response->status == 200 ? true : false; }
/** * @param $tmp * @param $upload */ function CloudStorage($tmp, &$upload) { global $zbp; $bucket = $zbp->Config('CloudStorage')->CS_Bucket; //云文件夹 $filename = date("Ymd", time()) . mt_rand(1000, 9999) . '_' . mt_rand(0, 1000) . '.' . GetFileExt($upload->SourceName); $object = $zbp->Config('CloudStorage')->CS_Dir . date("Y/m/", time()) . $filename; //构造云文件名 $file_path = $zbp->usersdir . 'upload/tmp.data'; //本地临时文件地址 @move_uploaded_file($tmp, $file_path); //先上传到本地 $upload->Name = $filename; switch ($zbp->Config('CloudStorage')->CS_Storage) { case '1': define('OSS_ACCESS_ID', $zbp->Config('CloudStorage')->CS_Ali_KeyID); //ACCESS_ID define('OSS_ACCESS_KEY', $zbp->Config('CloudStorage')->CS_Ali_KeySecret); //ACCESS_KEY require_once dirname(__FILE__) . '/api/oss/sdk.class.php'; $os_service = new ALIOSS(); $os_service->set_debug_mode(FALSE); //$os_service->set_enable_domain_style(TRUE);//二级域名 $response = $os_service->upload_file_by_file($bucket, $object, $file_path); //将本地文件上传到云 $upload->Metas->CS_URL = $response->header['_info']['url']; break; case '2': require_once dirname(__FILE__) . '/api/qiniu/io.php'; require_once dirname(__FILE__) . '/api/qiniu/rs.php'; $accessKey = $zbp->Config('CloudStorage')->CS_QNiu_KeyID; $secretKey = $zbp->Config('CloudStorage')->CS_QNiu_KeySecret; Qiniu_SetKeys($accessKey, $secretKey); $putPolicy = new Qiniu_RS_PutPolicy($bucket); $upToken = $putPolicy->Token(null); //$putExtra = new Qiniu_PutExtra(); //$putExtra->Crc32 = 1; list($ret, $err) = Qiniu_PutFile($upToken, $object, $file_path, null); $upload->Metas->CS_URL = 'http://' . $bucket . '.qiniudn.com/' . $ret['key']; //20140509更新V1.1:删除u break; case '3': define('BCS_AK', $zbp->Config('CloudStorage')->CS_Baidu_KeyID); //AK 公钥 define('BCS_SK', $zbp->Config('CloudStorage')->CS_Baidu_KeySecret); //SK 私钥 require_once dirname(__FILE__) . '/api/bcs/bcs.class.php'; $baidu_bcs = new BaiduBCS(); $object = "/" . $object; $response = $baidu_bcs->create_object($bucket, $object, $file_path, array('acl' => 'public-read')); //var_dump($response);die(); $upload->Metas->CS_URL = "http://bcs.duapp.com/" . $bucket . $object; //$response->header['_info']['url']; //未解决文件标识 break; default: break; } $upload->Metas->CS_Tpye = $zbp->Config('CloudStorage')->CS_Storage; unlink($file_path); //删除本地文件 $GLOBALS['Filter_Plugin_Upload_SaveFile']['CloudStorage'] = PLUGIN_EXITSIGNAL_RETURN; return true; }
public static function uploadFile($file, $content = null) { // 获取上传文件 if (empty($file['name'])) { return false; } $option = self::getConfig(); if (!isset($option->accessid)) { return false; } self::initSDK(); $obj = new ALIOSS($option->accessid, $option->accesskey, $option->endpoint); // 校验扩展名 $part = explode('.', $file['name']); $ext = ($length = count($part)) > 1 ? strtolower($part[$length - 1]) : ''; if (!Widget_Upload::checkFileType($ext)) { return false; } // 保存位置 $savename = str_replace(array('{year}', '{month}', '{day}'), array(date('Y'), date('m'), date('d')), ltrim(self::isImage($ext) ? $option->savepath : $option->nonimg_savepath, '/')) . sprintf('%u', crc32(uniqid())) . '.' . $ext; $response = $obj->upload_file_by_file($option->bucket, $savename, $file['tmp_name']); if ($response->status === 200) { return array('name' => $file['name'], 'path' => $savename, 'size' => $file['size'], 'type' => $ext, 'mime' => Typecho_Common::mimeContentType($savename)); } return false; }
function oss($file_path, $file_name) { require_once CSCMSPATH . 'uploads/oss/sdk.class.php'; $obj = new ALIOSS(); $dir = date('Ymd'); $response = $obj->create_object_dir(BUCKET, $dir); if ($response->status != '200') { return false; } $object = date('Ymd') . '/' . $file_name; $response = $obj->upload_file_by_file(BUCKET, $object, $file_path); if ($response->status == 200) { unlink($file_path); return true; } else { return false; } }
/** * 修改文件处理函数 * * @access public * @param array $content 老文件 * @param array $file 新上传的文件 * @return mixed */ public static function modifyHandle($content, $file) { if (empty($file['name'])) { return false; } $fileName = preg_split("(\\/|\\|:)", $file['name']); $file['name'] = array_pop($fileName); //获取扩展名 $ext = ''; $part = explode('.', $file['name']); if (($length = count($part)) > 1) { $ext = strtolower($part[$length - 1]); } if ($content['attachment']->type != $ext) { return false; } //获取文件名 $fileName = $content['attachment']->path; $path = $path . '/' . $fileName; //add for mkdir $path = substr($path, 1); $options = Typecho_Widget::widget('Widget_Options'); $access_id = $options->plugin('AliUpload')->access_id; $access_key = $options->plugin('AliUpload')->access_key; $bucket = $options->plugin('AliUpload')->bucket; $oss_service = new ALIOSS($access_id, $access_key); if (isset($file['tmp_name'])) { //移动上传文件 $response = $oss_service->upload_file_by_file($bucket, $path, $file['tmp_name']); if (!$response->isOk()) { return false; } } else { if (isset($file['bits'])) { //直接写入文件 $upload_file_options = array('content' => $file['bits'], 'length' => strlen($file['bits'])); $response = $oss_service->upload_file_by_content($bucket, $path, $upload_file_options); if (!$response->isOk()) { return false; } } else { return false; } } if (!isset($file['size'])) { $file['size'] = $response->header['_info']['size_upload']; } //返回相对存储路径 return array('name' => $content['attachment']->name, 'path' => $content['attachment']->path, 'size' => $file['size'], 'type' => $content['attachment']->type, 'mime' => $content['attachment']->mime); }
/** * 上传生成的缩略图到 OSS (并根据设定清理本地文件) * * @param $metadata * @return mixed */ function upload_thumb_2_oss($metadata) { if (preg_match('/\\d{4}\\/\\d{2}/', $metadata['file'], $m)) { $wp_uploads = wp_upload_dir($m[0]); } else { return $metadata; } $oss_options = get_option('oss_options', TRUE); $oss_bucket = esc_attr($oss_options['bucket']); $oss_ak = esc_attr($oss_options['ak']); $oss_sk = esc_attr($oss_options['sk']); $oss_upload_path = trim($oss_options['path'], '/'); $oss_nolocalsaving = esc_attr($oss_options['nolocalsaving']) == 'true' ? true : false; if (!is_object($aliyun_oss) && $oss_options['img_url'] == "") { $aliyun_oss = new ALIOSS($oss_ak, $oss_sk); } $opt['Expires'] = 'access plus 1 years'; if (isset($metadata['sizes']) && count($metadata['sizes']) > 0) { foreach ($metadata['sizes'] as $val) { $object = ltrim($oss_upload_path . '/' . trim($wp_uploads['subdir'], '/') . '/' . ltrim($val['file'], '/'), '/'); $file = $wp_uploads['path'] . '/' . $val['file']; if ($oss_options['img_url'] == "") { $aliyun_oss->upload_file_by_file($oss_bucket, $object, $file, $opt); } if ($oss_nolocalsaving) { _delete_local_file($file); } } } if ($oss_nolocalsaving) { $file = $wp_uploads['basedir'] . '/' . $metadata['file']; _delete_local_file($file); } return $metadata; }
/** * 上传 * * @param intval $uid uid 用户id * @param array $info ci 文件上传成功返回数据 * @param intval $id id 指定附件id * @return array */ public function upload($uid, $info, $id = 0) { $_ext = strtolower(substr($info['file_ext'], 1)); $author = $this->_get_member_name($uid); $replace = 0; $content = file_get_contents($info['full_path']); // 查询指定附件 if ($id) { $row = $this->db->where('id', $id)->get('attachment')->row_array(); if ($row) { $replace = 1; $this->siteid = intval($row['siteid']); } else { return '当前附件不存在'; } } // 入库附件 if (!$id) { $this->db->replace('attachment', array('uid' => (int) $uid, 'author' => $author, 'siteid' => $this->siteid, 'tableid' => 0, 'related' => '', 'fileext' => $_ext, 'filemd5' => $content ? md5($content) : 0, 'download' => 0, 'filesize' => $info['file_size'] * 1024)); $id = $this->db->insert_id(); // 入库失败,返回错误且删除附件 if (!$id) { @unlink($info['full_path']); return lang('m-145'); } } $remote = 0; $attachment = $file = (SYS_UPLOAD_DIR ? SYS_UPLOAD_DIR . '/' : '') . trim(substr($info['full_path'], strlen(SYS_UPLOAD_PATH)), '/'); // 远程附件信息 $remote_cfg = $this->ci->get_cache('attachment'); if (isset($remote_cfg[$this->siteid]['ext'][$_ext]) && ($rid = $remote_cfg[$this->siteid]['ext'][$_ext])) { $config = $remote_cfg[$this->siteid]['data'][$rid]; // 根据模式来存储 set_time_limit(0); if ($config['type'] == 1) { // ftp附件模式 $this->load->library('ftp'); if ($this->ftp->connect(array('port' => $config['value']['port'], 'debug' => FALSE, 'passive' => $config['value']['pasv'], 'hostname' => $config['value']['host'], 'username' => $config['value']['username'], 'password' => $config['value']['password']))) { // 连接ftp成功 $dir = basename(dirname($info['full_path'])) . '/'; $file = basename($info['full_path']); $path = $config['value']['path'] . '/' . $dir; $this->ftp->mkdir($path); if ($this->ftp->upload($info['full_path'], $path . $file, $config['value']['mode'], 0775)) { $remote = $config['id']; $attachment = $dir . $file; $file = $config['url'] . '/' . $attachment; unlink($info['full_path']); } $this->ftp->close(); } else { log_message('error', '远程附件ftp模式:ftp连接失败'); } } elseif ($config['type'] == 2) { // 百度云存储模式 $file = basename(dirname($info['full_path'])) . '/' . basename($info['full_path']); require_once FCPATH . 'dayrui/libraries/BaiduBCS/bcs.class.php'; $bcs = new BaiduBCS($config['value']['ak'], $config['value']['sk'], $config['value']['host']); $opt = array(); $opt['acl'] = BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_WRITE; $opt['curlopts'] = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 1800); $response = $bcs->create_object($config['value']['bucket'], '/' . $file, $info['full_path'], $opt); if ($response->status == 200) { $remote = $config['id']; $attachment = $file; $file = $config['url'] . '/' . $attachment; unlink($info['full_path']); } else { log_message('error', '远程附件百度云存储失败'); } } elseif ($config['type'] == 3) { // 阿里云存储模式 $file = basename(dirname($info['full_path'])) . '/' . basename($info['full_path']); require_once FCPATH . 'dayrui/libraries/AliyunOSS/sdk.class.php'; $oss = new ALIOSS($config['value']['id'], $config['value']['secret'], $config['value']['host']); $oss->set_debug_mode(FALSE); $response = $oss->upload_file_by_file($config['value']['bucket'], $file, $info['full_path']); if ($response->status == 200) { $remote = $config['id']; $attachment = $file; $file = $config['url'] . '/' . $attachment; unlink($info['full_path']); } else { log_message('error', '远程附件阿里云存储模式:' . $response->body); } } else { log_message('error', '远程附件类别(#' . (int) $config['type'] . ')未定义'); } } // 非远程附件补全本地地址 $file = !$remote ? SYS_ATTACHMENT_URL . $file : $file; $pos = strrpos($info['client_name'], '.'); $filename = strpos($info['client_name'], 'http://') === 0 ? trim(strrchr($info['client_name'], '/'), '/') : $info['client_name']; $filename = $pos ? substr($filename, 0, $pos) : $filename; if ($replace) { // 替换主表 $this->db->where('id', $id)->update('attachment', array('author' => $author, 'fileext' => $_ext, 'filemd5' => $content ? md5($content) : 0, 'filesize' => $info['file_size'] * 1024)); // 更新替换已使用的附件表 $this->db->where('id', $id)->update('attachment_' . $row['tableid'], array('uid' => $uid, 'author' => $author, 'remote' => $remote, 'fileext' => $_ext, 'filename' => $filename, 'filesize' => $info['file_size'] * 1024, 'attachment' => $attachment)); } else { // 增加至未使用附件表 $this->db->replace('attachment_unused', array('id' => $id, 'uid' => $uid, 'author' => $author, 'siteid' => $this->siteid, 'remote' => $remote, 'fileext' => $_ext, 'filename' => $filename, 'filesize' => $info['file_size'] * 1024, 'inputtime' => SYS_TIME, 'attachment' => $attachment, 'attachinfo' => '')); } return $replace ? $row : array($id, $file, $_ext); }