search
Homephp教程php手册【推荐】三秒教您把网站附件上传到腾讯云存储

教您把网站附件上传到腾讯云存储
50G附件以内免费使用,腾讯的,大于50G不建议使用
可能有些人会问: 安不安全啥的,这个我就不多说了,
你可以在本地备份一份,作为第二附附件存储,保证你的安全
案例 http://mp3.flash127.com

1.去腾讯云注册一个账号,产品服务->对象存储服务
注: 50G内免费 流量月50G 一般够用了哈
2.下载官方的sdk
3.将sdk解压,放到你的类库,这个自己定哈
4.我们上代码咯//thinkphp3.1.3的把引入类写在这里,3.2以上的,自己想办法哈<br> //注意,必须php5.3以上,因为他sdk要php5.3 硬要5.2自己改他sdk<br> <br> require('./Cos/include.php');<br> use Qcloud_cos\Auth; <br> use Qcloud_cos\Cosapi; <br> class UploadAction extends Action {<br>          //我们先从本地上传一份,当然你也可以不用,直接上传到COS 或者最后删除掉也行,自己决定<br>     public function upload() {<br>         import('ORG.Net.UploadFile');<br>         $upload = new UploadFile();// 实例化上传类<br>         $upload->maxSize  = 10485760 ;// 设置附件上传大小<br>         $upload->allowExts  = array('mp3');// 设置附件上传类型<br>         $upload->savePath =  './Uploads/'.Date('Ym').'/';// 设置附件上传目录<br>         $upload->saveRule= time(); //文件名<br>         if(!$upload->upload()) {// 上传错误提示错误信息<br>             $this->error($upload->getErrorMsg());<br>         }else{<br>             $info = $upload->getUploadFileInfo();<br>                 $this->cos_upload($info); //上传成功了,我们上传到cos<br>         }<br>     }<br> <br>         //上传到cos 这里我写好了目录建立代码咯,不要认为我发的代码跟sdk一样,啥都没写 想用的直接拿过去就可以用了<br>     public function cos_upload($info) {<br>         $srcPath=$info[0]['savepath'].$info[0]['savename']; <br>         $hash=$info[0]['hash']; //稀哈值<br>         <br>         $bucketName = "mp3data"; //Bucket名称<br>         $dar=Date('Ym'); //以年月为目录<br> <br>         //查询目录 如果无目录则创建目录<br>         $path = "/$dar/";<br>         $ispath=Cosapi::statFolder($bucketName, $path);<br>         if($ispath['code']!='0'){<br>             Cosapi::createFolder($bucketName, $path);//没有目录我就先创建<br>         }<br>         $dstPath = $path.time().".mp3"; //cos存储的路径,包括文件名与后缀,后缀自己想办法定义你上传的格式,我这只能上传mp3,所以我直接写上去了<br> <br>         $arr = Cosapi::upload($srcPath,$bucketName,$dstPath);<br>         if($arr['code']=='0'){//如果上传成功了<br>             $name = preg_replace('/.mp3/','',$_FILES['file']['name']);<br>             $data['name']=$name; //音乐名称<br>                 $data['host']='http://data.mp3.flash127.com'; //存储主机 附件在哪个主机上<br>             $data['url'] =$arr['data']['resource_path']; //cos存储路径<br>             $add=M('Music')->add($data);<br>             if($add){<br>                 unlink($srcPath);//上传成功了,我把本地文件删除,当然你也可以保留<br>                  $this->success('上传成功!', U('play/'.($add)) );<br>             }<br>         }else{<br>             <br>             exit('上传失败,'.$arr['message']);<br>             //这里自己定义操作,如果cos上传失败,你本地还有,你可以直接放入数据库,或者以后再同步也可以,<br>         }<br>     }<br> }<br> <br> <br> //本次代码结束,如有问题↓↓↓↓<br> <br> <br>  //----------------------------------------------------------<br>  //来源: 梦雪网络<br>  //作者: 小曾<br>  //扣扣: 839024615<br>  //交流群: 324098841<br>  //官网: www.php127.com<br>  //----------------------------------------------------------

AD:真正免费,域名+虚机+企业邮箱=0元

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use