function upload($source, $target) { try { $file = new UpyunMultiPartFile($source); } catch (Exception $e) { return 0; } if ($file->getSize() > 1024 * 1024 && $this->upyun_config['form_api_key']) { $sign = new UpyunMultipartSignature($this->upyun_config['form_api_key']); $upload = new UpyunMultipartUpload($sign); $upload->setBucketName($this->upyun_config['bucket_name']); $upload->setBlockSize($upload->getBlockSizeAdaptive($file)); try { $result = $upload->upload($file, array('path' => $this->upyun_dir . ltrim($target, '/'))); return $result; } catch (Exception $e) { return 0; } } else { $fh = fopen($source, 'rb'); if (!$fh) { return 0; } $upyun = new UpYun($this->upyun_config['bucket_name'], $this->upyun_config['operator_name'], $this->upyun_config['operator_pwd']); $rsp = $upyun->writeFile($this->upyun_dir . ltrim($target, '/'), $fh, true); return $rsp; } }
/** * undocumented function * * @return void * @author apple **/ function wtImage($filename, $data, $type = null) { $upyun = new UpYun('liunian-photo', '', ''); $type = is_null($type) ? 'image' : 'avatar'; $dir = is_null($type) ? '/img/' : '/avatar/'; switch ($type) { case 'image': $year = date('Y'); $month = date('m'); $path = "{$dir}{$year}/{$month}/{$filename}"; break; case 'avatar': $path = $dir . $filename; break; default: } // echo $data;exit; try { $rsp = $upyun->writeFile($path, $data, True); return TRUE; } catch (Exception $e) { return FALSE; // return $e->getCode().$e->getMessage(); } }
public function upload() { $upyun = new UpYun($this->user['extend']['bucket_name']['value'], SPACEOPERATORS, SPACEOPERATORSPASSWORD); try { //exit($this->user['extend']['bucket_name']['value']); $fh = fopen('sample.jpg', 'rb'); $rsp = $upyun->writeFile('/demo/bmw.flv', $fh, True); // 上传图片,自动创建目录 fclose($fh); var_dump($rsp); /** * md5上传 * Enter description here ... * @var unknown_type $opts = array( UpYun::CONTENT_MD5 => md5(file_get_contents("sample.jpg")) ); $fh = fopen('sample.jpg', 'rb'); $rsp = $upyun->writeFile('/demo/sample_md5.jpeg', $fh, True, $opts); // 上传图片,自动创建目录 fclose($fh); */ } catch (Exception $e) { echo $e->getCode(); echo $e->getMessage(); } }
function upload($file, $path) { if (!empty($file) && !empty($path)) { require_once 'upyun.class.php'; $upyun = new UpYun($this->bucket, $this->username, $this->password); try { $fh = fopen($file, 'rb'); $return = $upyun->writeFile($path, $fh, TRUE); fclose($fh); return $this->domain . $path; } catch (Exception $e) { //echo $e->getCode(); //echo $e->getMessage(); } } return FALSE; }
private function upload2upyun($file, $fileName) { // 上传到upyun $upyun = new UpYun(Typecho_Widget::widget('Widget_Options')->plugin('UEditor')->upyun_bucket, Typecho_Widget::widget('Widget_Options')->plugin('UEditor')->upyun_user, Typecho_Widget::widget('Widget_Options')->plugin('UEditor')->upyun_password); try { $fn = fopen($file, 'rb'); $rsp = $upyun->writeFile($fileName, $fn, True); fclose($fn); if ($rsp) { $this->stateInfo = $this->stateMap[0]; } else { $this->stateInfo = $this->getStateInfo("ERROR_UPYUN"); return false; } } catch (Exception $e) { $this->stateInfo = $this->getCode(); return false; } }
public function upload($file, $file_path) { require_once dirname(__FILE__) . '/../../../common/upyun.class.php'; $upyun = new UpYun('app-tools', 'apirl', 'pestalia'); if (is_file($file) && file_exists($file)) { try { echo "=========准备上传文件" . $file_path . "\r\n"; echo "=========设置MD5校验文件完整性\r\n"; $opts = array(UpYun::CONTENT_MD5 => md5(file_get_contents($file))); $fh = fopen($file, 'rb'); $rsp = $upyun->writeFile($file_path, $fh, True, $opts); fclose($fh); var_dump($rsp); echo "=========DONE\r\n\r\n"; } catch (Exception $e) { echo $e->getCode(); echo $e->getMessage(); } } }
public static function upload($from_file, $target_file) { if (empty($from_file) || empty($target_file)) { return false; } $unlink = 0; if (function_exists('option')) { $bucket = option('config.attachment_up_bucket'); $user = option('config.attachment_up_username'); $pwd = option('config.attachment_up_password'); $unlink = option('config.attachment_upload_unlink'); } else { $bucket = C('config.attachment_up_bucket'); $user = C('config.attachment_up_username'); $pwd = C('config.attachment_up_password'); $unlink = C('config.attachment_upload_unlink'); } if (empty($bucket) || empty($user) || empty($pwd)) { error_log('no auth'); return false; } $upyun = new UpYun($bucket, $user, $pwd); try { error_log($target_file); $fh = fopen($from_file, 'rb'); $rsp = $upyun->writeFile($target_file, $fh, true); fclose($fh); if ($unlink) { @unlink($from_file); } return true; } catch (Exception $e) { error_log($e->getCode()); error_log($e->getMessage()); return false; } }
} if (!$is_gifs) { imagedestroy($new_image); } } else { // 其它文件 // $rsp['msg'] = '本站暂不支持上传非图片附件'; // 涉及安全问题,别开放图片以外的文件上传 // 参见 http://youbbs.sinaapp.com/t-172 // if ($options['upyun_domain'] && $options['upyun_user'] && $options['upyun_pw']) { // 上传到又拍云 include dirname(__FILE__) . '/upyun.class.php'; $upyun = new UpYun($options['upyun_domain'], $options['upyun_user'], $options['upyun_pw']); // 本地调试失败 if ($upyun->writeFile('/' . $upload_filename, $_FILES['filetoupload']['tmp_name'], true)) { $rsp['status'] = 200; $rsp['url'] = '附件:' . $up_name . 'http://' . $options['upyun_domain'] . '.b0.upaiyun.com/' . $upload_filename; $rsp['msg'] = '附件已成功上传'; } else { $rsp['msg'] = '附件保存失败,请稍后再试'; } unset($out_img); } else { // 上传到服务器 if (move_uploaded_file($_FILES['filetoupload']['tmp_name'], $upload_filename)) { $rsp['status'] = 200; $rsp['url'] = '附件:' . $up_name . ' http://' . $_SERVER['HTTP_HOST'] . '/' . $upload_filename; $rsp['msg'] = '文件已成功上传'; } else { $rsp['msg'] = '文件保存失败,请稍后再试';
/** * 上传文件 * @access public * @param string $savePath 上传文件保存路径 * @return string * @throws ThinkExecption */ public function upload($savePath = '') { if (!$this->isOpen()) { $this->error = '没有开启云图片功能'; return false; } $fileInfo = array(); $isUpload = false; // 获取上传的文件信息,对$_FILES数组信息处理 $files = $this->dealFiles($_FILES); foreach ($files as $key => $file) { //过滤无效的上传 if (!empty($file['name'])) { $file['key'] = $key; $file['extension'] = $this->getExt($file['name']); if ($this->savePath) { $file['savepath'] = $this->savePath; } else { $file['savepath'] = $this->customPath; } if ($this->saveName) { $file['savename'] = $this->saveName; } else { $file['savename'] = uniqid() . "." . $file['extension']; } //移动设备上传的无后缀的图片,默认为jpg if ($GLOBALS['fromMobile'] == true && empty($file['extension'])) { $file['extension'] = 'jpg'; $file['savename'] = trim($file['savename'], '.') . '.jpg'; } elseif ($this->autoCheck) { if (!$this->check($file)) { return false; } } //计算hash if (function_exists($this->hashType)) { $fun = $this->hashType; $file['hash'] = $fun($file['tmp_name']); } //上传到云服务器 $config = $this->getConfig(); tsload(ADDON_PATH . '/library/upyun.class.php'); $cloud = new UpYun($config['cloud_image_bucket'], $config['cloud_image_admin'], $config['cloud_image_password']); $cloud->setTimeout(60); $file_content = file_get_contents($file['tmp_name']); $res = $cloud->writeFile('/' . $file['savepath'] . $file['savename'], $file_content, true); if (!$res) { $this->error = '上传到云服务器失败!'; return false; } //上传成功后保存文件信息,供其它地方调用 unset($file['tmp_name'], $file['error'], $file_content); $fileInfo[] = $file; $isUpload = true; } } if ($isUpload) { $this->uploadFileInfo = $fileInfo; return true; } else { $this->error = '上传出错!文件不符合上传要求。'; return false; } }
protected function _sendImageToYunServer($uploadList) { $upConf = C('upload'); if ($uploadList && $upConf['yun']['open']) { //开启云存储 T('image/upyun'); foreach ($uploadList as $key => $val) { try { $upyun = new UpYun($upConf['yun']['bucket'], $upConf['yun']['user'], $upConf['yun']['pwd']); $uploadList[$key]['savepath'] = $upConf['yun']['dir'] . ltrim($val['savepath'], $upConf['dir']); // z($uploadList[$key]['savepath']); $upyun->writeFile('/' . $uploadList[$key]['savepath'], file_get_contents(ROOT . $val['savepath']), true); // 上传图片,自动创建目录 } catch (Exception $e) { // echo $e->getCode().$e->getMessage(); } } } return $uploadList; }
function saveFile($filename, $data, $isAvatar = false, $sync = false) { $upConf = C('upload'); $fileInfo = array('status' => 0, 'width' => 0, 'height' => 0, 'type' => '', 'size' => strlen($data), 'url' => $filename); //临时处理,检查是否由于又拍云导致失败率飙升 if ($isAvatar && !$sync) { $upConf['yun']['open'] = false; } //临时处理,检查是否由于又拍云导致失败率飙升 if ($upConf['yun']['open']) { //开启云存储 T('image/upyun'); try { $upyun = new UpYun($upConf['yun']['bucket'], $upConf['yun']['user'], $upConf['yun']['pwd']); $fileInfo['url'] = $isAvatar ? $filename : $upConf['yun']['dir'] . $filename; $rsp = $upyun->writeFile('/' . $fileInfo['url'], $data, true); // 上传图片,自动创建目录 $fileInfo['status'] = 1; $fileInfo['width'] = $rsp['x-upyun-width']; $fileInfo['height'] = $rsp['x-upyun-height']; $fileInfo['type'] = strtolower($rsp['x-upyun-file-type']); } catch (Exception $e) { // echo $e->getCode().$e->getMessage(); } } else { //本地存储 $filename = $upConf['dir'] . ltrim(str_replace(array('./', '../'), '', $filename), $upConf['dir']); $fileInfo['url'] = $filename; setDir(dirname($filename)); if (file_put_contents(ROOT . $filename, $data)) { $fileInfo['status'] = 1; } } return $fileInfo; }
function mc_save_img_base64($img, $thumb = 2, $width = 400, $height = 400) { if (mc_user_id()) { $curDateTime = date("YmdHis"); $ymd = date("Ymd"); $randNum = rand(1000, 9999); $out_trade_no = mc_user_id() . $curDateTime . $randNum; $attached_type = ''; if (strstr($img, 'data:image/jpeg;base64,')) { $img_base = str_replace('data:image/jpeg;base64,', '', $img); $attached_type = 'jpg'; } elseif (strstr($img, 'data:image/png;base64,')) { $img_base = str_replace('data:image/png;base64,', '', $img); $attached_type = 'png'; } elseif (strstr($img, 'data:image/gif;base64,')) { $img_base = str_replace('data:image/gif;base64,', '', $img); $attached_type = 'gif'; } else { return $img; } if ($attached_type != '') { $img_decode = base64_decode($img_base); $fileName = 'Public/' . $ymd . '/' . $out_trade_no . '.' . $attached_type; // 获取需要创建的文件名称 if (!is_dir('Public/' . $ymd . '/')) { mkdir('Public/' . $ymd . '/', 0777); // 使用最大权限0777创建文件 } if (!file_exists($fileName)) { // 如果不存在则创建 // 检测是否有权限操作 if (!is_writeable($fileName)) { @chmod($fileName, 0777); // 如果无权限,则修改为0777最大权限 } // 最终将d写入文件即可 file_put_contents($fileName, $img_decode); if ($thumb == 1) { $image = new \Think\Image(); $image->open($fileName); $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_CENTER)->save($fileName); } } if (mc_option('upyun') == 2) { require_once 'upyun.class.php'; $upyun = new UpYun(mc_option('upyun_bucket'), mc_option('upyun_user'), mc_option('upyun_pwd')); $fh = fopen($fileName, 'rb'); $rsp = $upyun->writeFile('/img/' . $ymd . '/' . $out_trade_no . '.' . $attached_type, $fh, True); fclose($fh); $file_url = mc_option('upyun_url') . '/img/' . $ymd . '/' . $out_trade_no . '.' . $attached_type; } else { $file_url = mc_site_url() . '/' . $fileName; } $attached['src'] = $file_url; $attached['type'] = $attached_type; $result = M('attached')->data($attached)->add(); return $file_url; } } }
function kindedtiropic() { if ($this->upload_type == 'upyun') { $upyun_pic = new UpYun(UNYUN_BUCKET, UNYUN_USERNAME, UNYUN_PASSWORD, $api_access[0]); try { $api_access = array(UpYun::ED_AUTO, UpYun::ED_TELECOM, UpYun::ED_CNC, UpYun::ED_CTT); //$php_path = dirname(__FILE__) . '/'; //$php_url = dirname($_SERVER['PHP_SELF']) . '/'; //文件保存目录路径 //$save_path = $php_path . '../attached/'; //文件保存目录URL //$save_url = $php_url . '../attached/'; //$domain_file = $_config['file']['domain']; $domain_pic = 'http://' . UNYUN_DOMAIN; //$dir_file = $_config['file']['dir']; $dir_pic = '/' . $this->token . '/'; $save_path = ''; $save_url = ''; //定义允许上传的文件扩展名 $ext_arr = array('image' => explode(',', C('up_exts')), 'flash' => array('swf', 'flv'), 'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'), 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2')); //最大文件大小 $max_size = intval(C('up_size')) * 1000; //$save_path = realpath($save_path) . '/'; //PHP上传失败 if (!empty($_FILES['imgFile']['error'])) { switch ($_FILES['imgFile']['error']) { case '1': $error = '超过php.ini允许的大小。'; break; case '2': $error = '超过表单允许的大小。'; break; case '3': $error = '图片只有部分被上传。'; break; case '4': $error = '请选择图片。'; break; case '6': $error = '找不到临时目录。'; break; case '7': $error = '写文件到硬盘出错。'; break; case '8': $error = 'File upload stopped by extension。'; break; case '999': default: $error = '未知错误。'; } $this->alert($error); } //有上传文件时 if (empty($_FILES) === false) { //原文件名 $file_name = $_FILES['imgFile']['name']; //服务器上临时文件名 $tmp_name = $_FILES['imgFile']['tmp_name']; //文件大小 $file_size = $_FILES['imgFile']['size']; //检查文件名 if (!$file_name) { $this->alert("请选择文件。"); } //检查目录 //if (@is_dir($save_path) === false) { // alert("上传目录不存在。"); //} //检查目录写权限 //if (@is_writable($save_path) === false) { // alert("上传目录没有写权限。"); //} //检查是否已上传 if (@is_uploaded_file($tmp_name) === false) { $this->alert("上传失败。"); } //检查文件大小 if ($file_size > $max_size) { $this->alert("上传文件大小超过限制。"); } //检查目录名 $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']); if (empty($ext_arr[$dir_name])) { $this->alert("目录名不正确。"); } //获得文件扩展名 $temp_arr = explode(".", $file_name); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); //检查扩展名 if (in_array($file_ext, $ext_arr[$dir_name]) === false) { $this->alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。"); } //创建文件夹 if ($dir_name !== '') { $save_path .= $dir_name . "/"; $save_url .= $dir_name . "/"; //if (!file_exists($save_path)) { // mkdir($save_path); //} } $ymd = date("Ymd"); $save_path .= $ymd . "/"; $save_url .= $ymd . "/"; //if (!file_exists($save_path)) { // mkdir($save_path); //} //新文件名 $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext; //移动文件 $file_path = $save_path . $new_file_name; $fh = fopen($tmp_name, 'r'); $upyun_pic->writeFile($dir_pic . $file_path, $fh, True); $save_url = $domain_pic . $dir_pic . $save_url; fclose($fh); //if (move_uploaded_file($tmp_name, $file_path) === false) { // alert("上传文件失败。"); //} //@chmod($file_path, 0644); $file_url = $save_url . $new_file_name; header('Content-type: text/html; charset=UTF-8'); echo json_encode(array('error' => 0, 'url' => $file_url)); exit; } else { $this->alert('您就先别试这里了,我们服务器禁止写入文件了,O(∩_∩)O'); } } catch (Exception $e) { $this->alert($e->getCode() . ':' . $e->getMessage()); } } elseif ($this->upload_type == 'local') { $return = $this->localUpload(); if ($return['error']) { $this->alert($return['msg']); } else { header('Content-type: text/html; charset=UTF-8'); echo json_encode(array('error' => 0, 'url' => $return['msg'])); exit; } } }
function _loadimg($url) { global $db, $proverbkey; $upYun = new UpYun("imgbuket", "tolecen", "11311451167llx"); $fname = md5($url); $file = array(); $oldfile = $db->fetch_first("select * from " . tname("file") . " where SUBSTRING(file_key,12,32)='" . $fname . "'"); if ($oldfile['file_id'] > 0) { return array('id' => $oldfile['file_id'], 'farm' => 'farm1', 'bucket' => 'hbimg', 'key_path' => $oldfile['file_path'], 'key' => $oldfile['file_original'], 'type' => $oldfile['file_type'], 'width' => $oldfile['width'], 'height' => $oldfile['height']); exit; } $f = array('id' => 1, 'farm' => 'farm1', 'bucket' => 'hbimg', 'key_path' => '', 'key' => '', 'type' => 'image/jpeg', 'width' => '180', 'height' => '180'); $r = proverb_getimagesize($url); $imgtype = array('jpg' => '0', 'gif' => '1', 'png' => '2', 'bmp' => '3'); $stuffix = 'jpg'; if (isset($r[0]) && $r[0] > 0) { $c = "abcdefghigklmnopqrstuvwxyz0123456789"; $d = substr($c, rand(0, 20), 2) . '/' . substr($c, rand(0, 10), 2); $dir = ROOT_PATH . '/data/upload'; mkdir2($dir); switch ($r['mime']) { case 'image/gif': $stuffix = 'gif'; break; case 'image/png': $stuffix = 'png'; break; case 'image/bmp': $stuffix = 'bmp'; break; default: $stuffix = 'jpg'; break; } file_put_contents($dir . '/' . $fname . '.' . $stuffix, bidcms_fsockopen($url, $url)); $fileName = $fname . '.' . $stuffix; $filePath = str_replace(ROOT_PATH, "", $dir) . "/"; $filekey = $proverbkey . $imgtype[$stuffix] . $fname . str_replace('/', '', $d); $fh = fopen($dir . "/" . $fileName, 'r'); $upYun->writeFile("/" . $fileName, $fh); fclose($fh); if (is_file($dir . "/" . $fileName)) { @unlink($dir . "/" . $fileName); } $db->query("insert into " . tname("file") . "(`file_key`, `file_path`, `file_original`, `file_type`, `width`, `height`) values('" . $filekey . "','" . $filePath . "','" . $fileName . "','" . $r['mime'] . "','" . $r['0'] . "','" . $r[1] . "')"); $file_id = $db->insert_id(); if ($file_id) { $f = array('id' => $file_id, 'farm' => 'farm1', 'bucket' => 'hbimg', 'key_path' => $filePath, 'key' => $fileName, 'type' => $r['mime'], 'width' => $r[0], 'height' => $r[1]); } } return $f; }
public function sync($offset = 0) { if ($this->wxuser['winxintype'] < 3) { $this->error('没有权限'); } $material = new Material($this->wxuser); $params['post'] = json_encode(array('type' => 'image', 'offset' => $offset, 'count' => '10')); $result = $material->batchget($params); $dirname = '/uploads/' . substr($this->token, 0, 1) . '/' . $this->token . '/material/'; if ('local' == $this->upload_type) { $this->_mkdir('.' . $dirname); } $params['header'] = array('REFERER' => 'http://www.qq.com'); foreach ($result->item as $key => $value) { if (!empty($value->url)) { $file = D('Files')->where(array('sync_url' => $value->url, 'token' => $this->token))->find(); if (empty($file)) { $img = HttpClient::getInstance()->get($value->url, $params); $extension = pathinfo($value->name, PATHINFO_EXTENSION); $filename = $dirname . sha1($value->url) . '.' . $extension; if ('upyun' == $this->upload_type) { $upyun_pic = new UpYun(UNYUN_BUCKET, UNYUN_USERNAME, UNYUN_PASSWORD, $api_access[0]); try { $api_access = array(UpYun::ED_AUTO, UpYun::ED_TELECOM, UpYun::ED_CNC, UpYun::ED_CTT); $domain_pic = 'http://' . UNYUN_DOMAIN; $dir_pic = '/' . $this->token . '/material/' . sha1($value->url) . '.' . $extension; $upyun_pic->writeFile($dir_pic, $img, true); $filename = $domain_pic . $dir_pic; } catch (Exception $e) { echo json_encode(array('error' => 1, 'message' => $e->getCode() . ':' . $e->getMessage())); } } else { $size = file_put_contents('.' . $filename, $img); $filename = $this->siteUrl . $filename; } $Files = new Files(); $Files->index($filename, $size, $extension, $this->user['id'], $this->token, 0, $value->url, $value->media_id); } } } if (($offset + $result->item_count) < $result->total_count) { $this->success('正在同步 ' . $offset . ' - ' . ($offset + $result->item_count) . '张图片,请稍等...不要关闭此窗口', U('Upyun/sync', array('offset' => $offset + $result->item_count))); } else { $this->success('同步完成', U('Img/index')); } }
<?php /** * 设置默认头像,把默认头像 /static/default-avatar.png 上传到又拍云 */ define('IN_SAESPOT', 1); define('ROOT', pathinfo(__FILE__, PATHINFO_DIRNAME)); require ROOT . '/config.php'; require ROOT . '/common.php'; if (!$cur_user || $cur_user['flag'] < 99) { exit('error: 403 Access Denied'); } $tip = ""; if ($options['upyun_avatar_domain'] && $options['upyun_user'] && $options['upyun_pw']) { // 上传到又拍云 $out_img = file_get_contents('./static/default-avatar.png'); include ROOT . '/upyun.class.php'; $upyun = new UpYun($options['upyun_avatar_domain'], $options['upyun_user'], $options['upyun_pw']); if ($upyun->writeFile('/0.jpg', $out_img)) { $tip = '默认头像设置成功'; } else { $tip = '图片保存失败,请稍后再试'; } unset($out_img); } else { $tip = '还没设定头像空间名称和操作用户和密码'; } @header("content-Type: text/html; charset=UTF-8"); echo '<h3>', $tip, '</h3>'; echo '<h4><a href="/">返回首页</a></h4>';
<?php require_once '../upyun.class.php'; $upyun = new UpYun('bucket', 'dale', 'dalewu001'); try { echo "=========直接上传文件\r\n"; $fh = fopen('sample.jpeg', 'rb'); $rsp = $upyun->writeFile('/demo/sample_normal.jpeg', $fh, True); // 上传图片,自动创建目录 fclose($fh); var_dump($rsp); echo "=========DONE\n\r\n"; echo "=========设置MD5校验文件完整性\r\n"; $opts = array(UpYun::CONTENT_MD5 => md5(file_get_contents("sample.jpeg"))); $fh = fopen('sample.jpeg', 'rb'); $rsp = $upyun->writeFile('/demo/sample_md5.jpeg', $fh, True, $opts); // 上传图片,自动创建目录 fclose($fh); var_dump($rsp); echo "=========DONE\r\n\r\n"; echo "=========直接生成缩略图,不保存原图片,仅对图片文件有效\r\n"; $opts = array(UpYun::X_GMKERL_TYPE => 'square', UpYun::X_GMKERL_VALUE => 150, UpYun::X_GMKERL_QUALITY => 95, UpYun::X_GMKERL_UNSHARP => True); $fh = fopen('sample.jpeg', 'rb'); $rsp = $upyun->writeFile('/demo/sample_thumb_1.jpeg', $fh, True, $opts); // 上传图片,自动创建目录 fclose($fh); var_dump($rsp); echo "=========DONE\r\n\r\n"; echo "=========按照预先设置的缩略图类型生成缩略图类型生成缩略图,不保存原图,仅对图片空间有效\r\n"; $opts = array(UpYun::X_GMKERL_THUMBNAIL => 'thumbtype'); $fh = fopen('sample.jpeg', 'rb');
private function upload_crossdomian_xml($bucket_name) { $upyun = new UpYun($bucket_name, SPACEOPERATORS, SPACEOPERATORSPASSWORD); try { $file = CUR_CONF_PATH . 'data/crossdomain.xml'; $fh = fopen($file, 'rb'); $rsp = $upyun->writeFile('/crossdomain.xml', $fh, True); // 上传图片,自动创建目录 fclose($fh); } catch (Exception $e) { // } }
function upyun($file_path, $file_name) { require_once CSCMSPATH . 'uploads/upyun/upyun.class.php'; $upyun = new UpYun(CS_Upy_Bucket, CS_Upy_Name, CS_Upy_Pwd); $object = '/' . date('Ymd') . '/' . $file_name; $data = @file_get_contents($file_path); $response = $upyun->writeFile($object, $data, True); if ($response) { unlink($file_path); return true; } else { return false; } }
* array( * "originalName" => "", //原始文件名 * "name" => "", //新文件名 * "url" => "", //返回的地址 * "size" => "", //文件大小 * "type" => "" , //文件类型 * "state" => "" //上传状态,上传成功时必须返回"SUCCESS" * ) */ $info = $up->getFileInfo(); $upyun = new UpYun($file_bucketname, $file_username, $file_password); try { $con = $info["url"]; $opts = array(UpYun::CONTENT_MD5 => md5(file_get_contents($con))); $fh = fopen($con, "rb"); $rsp = $upyun->writeFile("/" . $con, $fh, True, $opts); // 上传图片,自动创建目录 fclose($fh); } catch (Exception $e) { $filename = "error_log.txt"; $errorcontent = "file " . date("Y-m-d H:m:s") . " " . $e->getCode() . " " . $e->getMessage() . "\r\n"; $handle = fopen($filename, 'a'); fwrite($handle, $errorcontent); fclose($handle); exit; } /** * 向浏览器返回数据json数据 * { * 'url' :'a.rar', //保存后的文件路径 * 'fileType' :'.rar', //文件描述,对图片来说在前端会添加到title属性上
$config = (require_once './config.php'); require_once './upyun-php-sdk/upyun.class.php'; if (isset($_POST['text'])) { $text = isset($_POST['text']) ? $_POST['text'] : ""; $footer = isset($_POST['footer']) ? $_POST['footer'] : ""; $by = isset($_POST['by']) ? $_POST['by'] : '由scuinfo.com根据热门程度自动生成,并不一定同意此观点! ' . "\n" . '微信关注scuinfo后可直接匿名发布内容到scuinfo.com'; $transform = new Text2pic\Transform($by, './uploads', 'http://docker.dev/uploads'); $result = $transform->generate($text, $footer); if ($result['code'] == 200) { //到这里图片已生成,下面是上传到upyun的代码 $upyun = new UpYun($config['upyun_bucket'], $config['upyun_user'], $config['upyun_password']); try { $opts = array(UpYun::CONTENT_MD5 => md5(file_get_contents($result['data']['path']))); $fh = fopen($result['data']['path'], 'rb'); $fileName = '/uploads/' . md5($result['data']['path']) . '.jpg'; $rsp = $upyun->writeFile($fileName, $fh, True, $opts); // 上传图片,自动创建目录 fclose($fh); unlink($result['data']['path']); //删除服务器的图片 $result = array("code" => 200, "message" => "ok", "data" => array("url" => $config['upyun_base_pic_url'] . $fileName)); echo json_encode($result, JSON_UNESCAPED_UNICODE); } catch (Exception $e) { $result = array("code" => 2003, "message" => $e->getMessage()); echo json_encode($result, JSON_UNESCAPED_UNICODE); } } else { $result = array("code" => 2002, "message" => $result['message']); echo json_encode($result, JSON_UNESCAPED_UNICODE); } } else {
public function kindedtiropic() { if ($this->upload_type == 'upyun') { $upyun_pic = new UpYun(UNYUN_BUCKET, UNYUN_USERNAME, UNYUN_PASSWORD, $api_access[0]); try { $api_access = array(UpYun::ED_AUTO, UpYun::ED_TELECOM, UpYun::ED_CNC, UpYun::ED_CTT); $domain_pic = 'http://' . UNYUN_DOMAIN; $dir_pic = '/' . $this->token . '/'; $save_path = ''; $save_url = ''; $ext_arr = array('image' => explode(',', C('up_exts')), 'flash' => array('swf', 'flv'), 'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'), 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2')); $max_size = intval(C('up_size')) * 1000; if (!empty($_FILES['imgFile']['error'])) { switch ($_FILES['imgFile']['error']) { case '1': $error = '超过php.ini允许的大小。'; break; case '2': $error = '超过表单允许的大小。'; break; case '3': $error = '图片只有部分被上传。'; break; case '4': $error = '请选择图片。'; break; case '6': $error = '找不到临时目录。'; break; case '7': $error = '写文件到硬盘出错。'; break; case '8': $error = 'File upload stopped by extension。'; break; case '999': default: $error = '未知错误。'; } $this->alert($error); } if (empty($_FILES) === false) { $file_name = $_FILES['imgFile']['name']; $tmp_name = $_FILES['imgFile']['tmp_name']; $file_size = $_FILES['imgFile']['size']; if (!$file_name) { $this->alert('请选择文件。'); } if (@is_uploaded_file($tmp_name) === false) { $this->alert('上传失败。'); } if ($max_size < $file_size) { $this->alert('上传文件大小超过限制。'); } $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']); if (empty($ext_arr[$dir_name])) { $this->alert('目录名不正确。'); } $temp_arr = explode('.', $file_name); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); if (in_array($file_ext, $ext_arr[$dir_name]) === false) { $this->alert('上传文件扩展名是不允许的扩展名。' . "\n" . '只允许' . implode(',', $ext_arr[$dir_name]) . '格式。'); } if ($dir_name !== '') { $save_path .= $dir_name . '/'; $save_url .= $dir_name . '/'; } $ymd = date('Ymd'); $save_path .= $ymd . '/'; $save_url .= $ymd . '/'; $new_file_name = date('YmdHis') . '_' . rand(10000, 99999) . '.' . $file_ext; $file_path = $save_path . $new_file_name; $fh = fopen($tmp_name, 'r'); $upyun_pic->writeFile($dir_pic . $file_path, $fh, true); $save_url = $domain_pic . $dir_pic . $save_url; fclose($fh); $file_url = $save_url . $new_file_name; header('Content-type: text/html; charset=UTF-8'); echo json_encode(array('error' => 0, 'url' => $file_url)); exit; } else { $this->alert('您就先别试这里了,我们服务器禁止写入文件了,O(∩_∩)O'); } } catch (Exception $e) { $this->alert($e->getCode() . ':' . $e->getMessage()); } } else { if ($this->upload_type == 'local') { $return = $this->localUpload(); if ($return['error']) { $this->alert($return['msg']); } else { header('Content-type: text/html; charset=UTF-8'); echo json_encode(array('error' => 0, 'url' => $return['msg'])); exit; } } } }
} $new_image = imagecreatetruecolor($new_w, $new_h); $bg = imagecolorallocate($new_image, 255, 255, 255); imagefill($new_image, 0, 0, $bg); ////目标文件,源文件,目标文件坐标,源文件坐标,目标文件宽高,源宽高 imagecopyresampled($new_image, $img_obj, 0, 0, 0, 0, $new_w, $new_h, $img_info[0], $img_info[1]); imagedestroy($img_obj); // 上传到又拍云 include ROOT . '/upyun.class.php'; ob_start(); imagejpeg($new_image, NULL, 95); $out_img = ob_get_contents(); ob_end_clean(); $upyun = new UpYun($options['upyun_avatar_domain'], $options['upyun_user'], $options['upyun_pw']); // 本地调试失败 if ($upyun->writeFile('/' . $cur_uid . '.jpg', $out_img)) { if ($cur_user['avatar'] != $cur_user['id']) { $DBM = new DB_MySQL(); $DBM->connect($servername_m, $dbport, $dbusername, $dbpassword, $dbname); if ($DBM->unbuffered_query("UPDATE `yunbbs_users` SET `avatar`='{$cur_uid}' WHERE `id`='{$cur_uid}'")) { $cur_user['avatar'] = $cur_user['id']; $MMC->set('u_' . $cur_uid, $cur_user, 0, 600); } else { $tip2 = '数据保存失败,请稍后再试'; } } } else { $tip2 = '图片保存失败,请稍后再试'; } unset($out_img); //
function log($log) { $log_file = "log.txt"; $handle = fopen($log_file, "a"); fwrite($handle, $log); fclose($handle); exit; } */ $upyun = new UpYun($bucketname, $username, $password); try { $uri = strstr($info["url"], "upload"); if (file_exists($uri)) { $opts = array(UpYun::CONTENT_MD5 => md5(file_get_contents($uri))); $fh = fopen($uri, "rb"); $rsp = $upyun->writeFile("/ueditor/php/" . $uri, $fh, True, $opts); fclose($fh); } else { $log = date("Y-m-d H:m:s") . " 文件不存在,请检查目录是否正确。" . "\r\n"; $log_file = "log.txt"; $handle = fopen($log_file, "a"); fwrite($handle, $log); fclose($handle); exit; } } catch (Exception $e) { $log = date("Y-m-d H:m:s") . " " . $e->getCode() . " " . $e->getMessage() . "\r\n"; $log_file = "log.txt"; $handle = fopen($log_file, "a"); fwrite($handle, $log); fclose($handle);
/** * 把微信的图片上传到又拍云服务器上 * @param string $medil * @return string */ public function updateWxImage($medil) { if (substr($medil, 0, 4) == 'http') { return $medil; } $accesstoken = WX::actionAccessToken(); $imgurl = "https://api.weixin.qq.com/cgi-bin/media/get?access_token={$accesstoken}&media_id={$medil}"; $upyun = new UpYun(UPYUN_BUCKET, UPYUN_USER, UPYUN_PASSWORD); $fh = file_get_contents($imgurl); if (empty($fh)) { return ""; } $md5 = md5($this->openid); $path = "/" . substr($md5, 0, 4) . '/' . $md5 . '/' . $medil . '.jpg'; $rsp = $upyun->writeFile($path, $fh, True); // 上传图片,自动创建目录 return UPYUN_CDN . $path; }
public function upyun() { ignore_user_abort(true); set_time_limit(0); $cfg_up = (require CONFIG . '/upyun.config.php'); $upyun = new \UpYun($cfg_up['bucket'], $cfg_up['user'], $cfg_up['pswd'], \UpYun::ED_TELECOM); $opts = array(\UpYun::X_GMKERL_THUMBNAIL => 'square'); $lo_file = UPLOADS . '/post/14.jpg'; $up_file = '/dddd/ok/6duplodaddd.jpg'; $fh = fopen($lo_file, 'r'); $upyun->writeFile($up_file, $fh, true, $opts); $up_url = "http://" . $cfg_up['bucket'] . '.b0.upaiyun.com' . $up_file; fclose($fh); }
if (!file_exists($save_path)) { mkdir($save_path); } //新文件名 $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext; //移动文件 $file_path = $save_path . $new_file_name; if (move_uploaded_file($tmp_name, $file_path) === false) { alert("上传文件失败。"); } @chmod($file_path, 0644); //$file_url = mc_site_url() . '/' . 'Public/' . $file_ext . '/' . $ymd . '/' . $new_file_name; //$file_url_db = 'Public/' . $file_ext . '/' . $ymd . '/' . $new_file_name; try { $fh = fopen($file_path, 'rb'); $rsp = $upyun->writeFile('/img/' . $ymd . '/' . $new_file_name, $fh, True); // 上传图片,自动创建目录 fclose($fh); //var_dump($rsp); } catch (Exception $e) { //echo $e->getCode(); //echo $e->getMessage(); } $file_url = mc_site_url() . '/' . 'Public/' . $file_ext . '/' . $ymd . '/' . $new_file_name; $file_url_db = mc_option('upyun_url') . '/img/' . $ymd . '/' . $new_file_name; //mc媒体库写入 $Data = M('attached'); $attached['src'] = $file_url_db; $attached['type'] = $file_ext; $result = $Data->data($attached)->add(); header('Content-type: text/html; charset=UTF-8');