简化使用七牛云存储实现文件上传功能<?php <br />
<br>
/**<br>
* 上传七牛<br>
*<br>
* @author huqitao <huqitaoit><br>
*/<br>
<br>
namespace Common\Utility;<br>
<br>
class UploadImg {<br>
<br>
protected $config;<br>
protected $domain;<br>
protected $bucket;<br>
<br>
public function __construct($domain, $bucket) {<br>
$this->domain = $domain;<br>
$this->bucket = $bucket;<br>
$this->config = array(<br>
'maxSize' => 2 * 1024 * 1024, //文件大小<br>
'rootPath' => './',<br>
'saveName' => array('uniqid', ''),<br>
'driver' => 'Qiniu',<br>
'driverConfig' => array(<br>
'secretKey' => '######', //七牛空间配置参数<br>
'accessKey' => '########',<br>
'domain' => $this->domain, //空间地址<br>
'bucket' => $this->bucket, //空间名称<br>
)<br>
);<br>
}<br>
<br>
<br>
<br>
/**<br>
* 上传一个文件<br>
* @param array $file 文件参数<br>
* @return array 返回 code 与文件路径或错误信息<br>
*/<br>
public function uploadOne($file) {<br>
$upload = new \Think\Upload($this->config);<br>
$info = $upload->uploadOne($file);<br>
if (!$info) {<br>
return [550, $upload->getError()];<br>
}<br>
return [200, $info['url']]; <br>
}<br>
/**<br>
* 上传多图<br>
* @param type $files<br>
* @return array 返回 code 与文件路径数组 或错误信息<br>
*/<br>
public function uploads($files) {<br>
$upload = new \Think\Upload($this->config);<br>
$info = $upload->upload($files);<br>
if (!$info) {<br>
return [550, $upload->getError()];<br>
}<br>
foreach ($info as $v) {<br>
$pArray[] = "http://".$this->domain."/".strtr($v['name'], '/', '_');<br>
}<br>
return [200, $pArray];<br>
}<br>
<br>
}</huqitaoit>
使用方法,单图上传:public function uploadSchool() {<br>
if (IS_POST) {<br>
$domain = "#####";<br>
$bucket = "###";<br>
if (empty($_FILES)) {<br>
$this->ajaxReturn(makeinformation(550, "没有上传图片"));<br>
}<br>
$uploadImg = new \Common\Utility\UploadImg($domain, $bucket);<br>
$data = $uploadImg->uploadOne($_FILES['file']);<br>
if ($data[0] != 200) {<br>
$this->ajaxReturn(makeinformation(550, $data[1]));//失败返回错误<br>
}<br>
$this->ajaxReturn(makeinformation(200, '', array('url' => $data[1]))); //成功返回图片绝对地址<br>
}<br>
}
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
