控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*应用服务器php返回签名*/
class Oss extends CI_Controller{
private $id;
private $key;
private $host;
private $dir;
public function __construct()
{
parent::__construct();
$this->load->library('oss/get');
$this->id = $this->config->item('oss_access_keyid'); // AccessKeyId
$this->key = $this->config->item('oss_access_keysecret'); // AccessKeySecret
$this->host = $this->config->item('oss_up_host'); // OSS 访问域名
$this->dir = $this->input->get('dir'); // 文件前缀名
}
// 上传主页面
public function index()
{
if(empty($this->dir)) {
echo 'dir is null';
} else {
$data = array();
$data['callbackurl'] = '/index.php/oss/get_secret?dir='.$this->dir;
$this->load->view('oss/index',$data);
}
}
// 获取上传密钥
public function get_secret()
{
if(empty($this->dir)) {
echo 'dir is null';
} else {
$this->get->response($this->id,$this->key,$this->host,$this->dir);
}
}
}
视图:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>OSS web直传</title>
<link rel="stylesheet" type="text/css" href="/oss/style.css"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>
<body>
<h2>OSS web直传---在服务端php签名</h2>
<ol>
<li>基于plupload封装 </li>
<li>支持html5,flash,silverlight,html4 等协议上传</li>
<li>可以运行在PC浏览器,手机浏览器,微信</li>
<li>签名在服务端(php)完成, 安全可靠, 推荐使用!</li>
<li>可以选择多文件上传</li>
<li>显示上传进度条</li>
<li>可以控制上传文件的大小</li>
<li>最关键的是,让你10分钟之内就能移植到你的系统,实现以上牛逼的功能!</li>
<li>注意一点,bucket必须设置了Cors(Post打勾),不然没有办法上传</li>
</ol>
<br>
<h4>您所选择的文件列表:</h4>
<div id="ossfile">你的浏览器不支持flash,Silverlight或者HTML5!</div>
<br/>
<div id="container">
<a id="selectfiles" href="javascript:void(0);" class='btn'>选择文件</a>
<a id="postfiles" href="javascript:void(0);" class='btn'>开始上传</a>
</div>
<pre id="console"></pre>
<p> </p>
<input id="callbackurl" type="hidden" value="<?php echo $callbackurl;?>"><!--此url为获取签名地址-->
</body>
<script type="text/javascript" src="/oss/lib/plupload-2.1.2/js/plupload.full.min.js"></script>
<script type="text/javascript" src="/oss/upload.js"></script>
</html>
lib类
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Get {
/**
* 应用服务器php返回签名
*
* @param $id AccessKeyId
* @param $key AccessKeySecret
* @param $host OSS 访问域名
* @param $dir 文件保存目录,建议以活动为一个单位
*/
public function response($id,$key,$host,$dir)
{
require_once 'oss_php_sdk_20140625/sdk.class.php';
$now = time();
$expire = 30; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
$end = $now + $expire;
$expiration = $this->gmt_iso8601($end);
$oss_sdk_service = new alioss($id, $key, $host);
//最大文件大小.用户可以自己设置
$condition = array(0=>'content-length-range', 1=>0, 2=>1048576000);
$conditions[] = $condition;
//表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
$start = array(0=>'starts-with', 1=>'$key', 2=>$dir);
$conditions[] = $start;
//这里默认设置是2020年.注意了,可以根据自己的逻辑,设定expire 时间.达到让前端定时到后面取signature的逻辑
$arr = array('expiration'=>$expiration,'conditions'=>$conditions);
//echo json_encode($arr);
//return;
$policy = json_encode($arr);
$base64_policy = base64_encode($policy);
$string_to_sign = $base64_policy;
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true));
$response = array();
$response['accessid'] = $id;
$response['host'] = $host;
$response['policy'] = $base64_policy;
$response['signature'] = $signature;
$response['expire'] = $end;
//这个参数是设置用户上传指定的前缀
$response['dir'] = $dir;
echo json_encode($response);
}
private function gmt_iso8601($time) {
$dtStr = date("c", $time);
$mydatetime = new DateTime($dtStr);
$expiration = $mydatetime->format(DateTime::ISO8601);
$pos = strpos($expiration, '+');
$expiration = substr($expiration, 0, $pos);
return $expiration."Z";
}
}
?>