今天给大家实现一个头像上传功能,需要将文件上传至阿里云的OSS,所以也是百度、谷歌了一番,但都不是很管用,所以自己研究了一番,下面向大家分享这个过程,在这之前先下载阿里云OSS的SDK。 文件目录如下
先拷贝sdk到上传控制器的同级目录
新建一个一个简单的HTML,传递文件到控制器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="width: 100%; height: 100%;">
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">文件名:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="提交">
</form>
</div>
</body>
</html>
拷贝sdk中samples目录下的Config.php和Common.php到控制器的统计目录,修改Config.php代码如下
<?php
final class Config
{
const OSS_ACCESS_ID = 'update me';
const OSS_ACCESS_KEY = 'update me';
const OSS_ENDPOINT = 'update me';
const OSS_TEST_BUCKET = 'update me';
}
第一个填写你的阿里云AccessKey ID,第二个填写你的阿里云secret,这个咋阿里云的AccessKey 管理可以查到,第三个在oss那里可以查到 第四个是你Bucket的名字应该不用我多说了 Common.php的代码修改引入的代码也就是前面的6行,代码如下:
<?php
if (is_file(__DIR__ . '/aliyun-php-sdk-oss/autoload.php')) {
require_once __DIR__ . '/aliyun-php-sdk-oss/autoload.php';
}
if (is_file(__DIR__ . '/aliyun-php-sdk-oss/vendor/autoload.php')) {
require_once __DIR__ . '/aliyun-php-sdk-oss/vendor/autoload.php';
}
require_once __DIR__ . '/Config.php';
use OSS\OssClient;
use OSS\Core\OssException;
class Common
{
const endpoint = Config::OSS_ENDPOINT;
const accessKeyId = Config::OSS_ACCESS_ID;
const accessKeySecret = Config::OSS_ACCESS_KEY;
const bucket = Config::OSS_TEST_BUCKET;
public static function getOssClient()
{
try {
$ossClient = new OssClient(self::accessKeyId, self::accessKeySecret, self::endpoint, false);
} catch (OssException $e) {
printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
printf($e->getMessage() . "\n");
return null;
}
return $ossClient;
}
public static function getBucketName()
{
return self::bucket;
}
public static function createBucket()
{
$ossClient = self::getOssClient();
if (is_null($ossClient)) exit(1);
$bucket = self::getBucketName();
$acl = OssClient::OSS_ACL_TYPE_PUBLIC_READ;
try {
$ossClient->createBucket($bucket, $acl);
} catch (OssException $e) {
$message = $e->getMessage();
if (\OSS\Core\OssUtil::startsWith($message, 'http status: 403')) {
echo "Please Check your AccessKeyId and AccessKeySecret" . "\n";
exit(0);
} elseif (strpos($message, "BucketAlreadyExists") !== false) {
echo "Bucket already exists. Please check whether the bucket belongs to you, or it was visited with correct endpoint. " . "\n";
exit(0);
}
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}
public static function println($message)
{
if (!empty($message)) {
echo strval($message) . "\n";
}
}
}
添加控制器代码,upload.php:
<?php
require_once __DIR__ . '/Common.php';
use OSS\OssClient;
$bucketName = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);
$file=$_FILES['file'];
$info = pathinfo($file["name"]);
$ext = $info['extension'];
$file_name = date( "YmdHis" ) . time() . mt_rand(100000,999999) . ".{$ext}" ;
$res=$ossClient->uploadFile($bucketName, $file_name,$file["tmp_name"]);
if($res["info"]["http_code"]==200){
die("上传成功");
}else{
die("上传失败");
}
登陆阿里云的后台可以查看文件上传上去了没有。
|