Thinkphp的OSS存储上传类库案例
本文章主要介绍Thinkphp框架利用OSS存储API上传demo。
环境: httpd-2.2.24 、mysql- 5.5.18、php-5.3.27。(linux) 环境安装指导
thinkphp版本:3.1.2
oss版本:PHP SDK V2 开发包
将 PHP SDK V2 开发包 放入 /ThinkPHP/Extend下
在控制器加载调用
1 2 3 |
date_default_timezone_set('Asia/Shanghai'); Vendor('aliyun.aliyun'); use Aliyun\OSS\OSSClient; |
方法中调用(上传图片)更多oss方法
1 2 3 4 5 6 7 8 9 10 |
$client = OSSClient::factory(array( 'AccessKeyId' => 'xxxxxxxx', 'AccessKeySecret' => 'xxxxxxxxx', )); $client->putObject(array( 'Bucket' => '自取的名字', 'Key' => $new_name, 'Content' => fopen($big_image_info['tmp_name'], 'r'), 'ContentLength' => filesize($big_image_info['tmp_name']), )); |
全部代码(控制器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<?php /** * * @author LiZeQiao <674531003@qq.com> * @version v1.0.0 * @description */ date_default_timezone_set('Asia/Shanghai'); Vendor('aliyun.aliyun'); use Aliyun\OSS\OSSClient; class TestAction extends CommonAction{ public function index(){ $this->display(); } public function do_test(){ //.获取要上传文件的信息 $big_image_info= $_FILES['big_image']; $typelist=array("image/gif","image/jpeg","image/jpg","image/png"); //定义运行的上传文件类型 /*获取后缀名 start*/ $hc_big=explode('.',$big_image_info['name']); $big_aaa=$hc_big['1']; /*获取后缀名 end*/ //.判断文件是否上传错误 if($big_image_info['error'] <= 0){ //.判断文件上传的类型是否合法 if(in_array($big_image_info['type'],$typelist)){ //.上传文件的大小过滤 if($big_image_info['size']<9000000){ //.上传文件名处理 $new_name = time().'.'.$big_aaa; //。判断是否是上传的文件,并执行上传 if(is_uploaded_file($big_image_info['tmp_name'])){ $client = OSSClient::factory(array( 'AccessKeyId' => 'xxxxxxx', 'AccessKeySecret' => 'xxxxxxxx', )); $client->putObject(array( 'Bucket' => '自取的名称', 'Key' => $new_name, 'Content' => fopen($big_image_info['tmp_name'], 'r'), 'ContentLength' => filesize($big_image_info['tmp_name']), )); }else{ $error = "图片文件不是上传的文件"; return; } }else{ $error = "图片太大"; return; } }else{ $error = "图片文件类型错误"; return; } }else{ $error = "图片文件上传错误"; return; } $new_name='http://'.'自取的名称'.'.oss-cn-hangzhou.aliyuncs.com/'.$new_name; var_dump($new_name); } } |
网盘下载: http://pan.baidu.com/s/1hqjC7ic
- “苹果表Watch”强势进攻,Google怎样应战?
- SEO核心内容是网站优化分析