模板兔在给客户开发一个网站的过程中,发现一个现象就是几乎所有的wordpress七牛插件都是走媒体库中转上传的,那么在上传大文件的时候就有可能卡死。那么如何实现文件直传到七牛呢?
首先,下载官方SDK,github.com/qiniu/php-sdk 官方文档 developer.qiniu.com/kodo/sdk/php
前端index.html
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <table> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="image"> <input type="submit" value="上传"> </form> </table> </body> </html>
后端upload.php
<?php
require_once __DIR__ . '/qiniusdk/autoload.php'; //引入加载文件
use Qiniu\Auth; //使用auth类
use Qiniu\Storage\UploadManager;
$accessKey = '****';
$secretKey = '****';
$auth = new Auth($accessKey, $secretKey); //实例化
$bucket='****';//存储空间
$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
$filePath = $_FILES['image']['tmp_name'];
if($_FILES['image']['type']=='video/mp4'){
$key = 'video'.time().'.mp4';
}elseif($_FILES['image']['type']=='audio/mp3'){
$key = 'audio'.time().'.mp3';
}else{
$key = 'png'.time().'.png';
}
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
if ($err !== null) {
echo '上传失败';
} else{
print_r($ret['key']);//上传后的文件名
}


0 个评论