php 文件上传至OSS及删除远程阿里云OSS文件

时间:2022-05-10 17:03:55

删除的时候记住不要带域名,也不要带'/'这个路径符号,如全路径是:'https://hxty.oss-cn-beijing.aliyuncs.com/data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf',则这里的path_file路径为:'data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf'。

?
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
include './aliyun-oss-php-sdk-2.4.2/autoload.php';
 
use OSS\OssClient;
use OSS\Core\OssException;
 
error_reporting(E_ALL);
ini_set('display_errors', 1); //错误信息
ini_set('display_startup_errors', 1); //php启动错误信息
session_start();
/**
 * 取小数点后两位
 * @param type $str 带小数的数字
 * @return type     返回准确截取两位的小数
 */
 
function num_format($str) {
    if (strpos($str, '.') === false) {
        return $str;
    }
    $retArr = explode(".", $str);
    $strs = substr($retArr[1], 0, 2);
    return $retArr[0] . "." . $strs;
}
 
function create_folders($dir) {
    return is_dir($dir) or ( create_folders(dirname($dir)) and mkdir(iconv("UTF-8", "GBK", $dir), 0777, true));
}
$file = $_FILES['file'];
 
$accessKeyId = '';//RAM alioss后台能拿到
$accessKeySecret = '';//RAM alioss后台能拿到
$endpoint = 'oss-cn-beijing.aliyuncs.com';//alioss后台 概览可以拿到
$bucket = 'planet-cross';//创建的组,随便吧创建,权限正常就行
echo '<pre>';
var_dump($_SESSION);
if ($file) {
    $filename = "/upload/image/" . date("Ymd");
    if (!file_exists("." . $filename)) {
        create_folders("." . $filename);
    }
//获取后缀
    $ary = explode('.', $file['name']);
    $su = end($ary);
//文件名
    $filenames = $filename . "/" . time() . rand(1, 10000) . "." . $su;
    move_uploaded_file($file['tmp_name'], "." . $filenames);
 
    $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $filenames = ltrim($filenames, '/'); //去除 第一个 '/'
// 开始上传
    $pic = $filenames;
    try {
        $oss = $ossClient->uploadFile($bucket, $pic, $filenames);
        $filenames = $oss['info']['url'];
        $_SESSION['file']=$filenames;
        echo $filenames;
        echo '<img src="' . $filenames . '" />';
        //删除本地文件
    } catch (\OSS\Core\OssException $e) {
        die("Failed to upload file ");
        return false;
    }
} else if(!empty($_SESSION['file'])){
    try {
        $patch=trim(parse_url($_SESSION['file'])['path'],'/');
        $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $oss=$ossClient->deleteObject($bucket,$patch);
        $_SESSION['file']=null;
    } catch (OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
}
 
 
    /**
     * 删除文件
     * 可以删除调用ajaxfile上传的文件
     * @param type $path_file   文件路径
     * 删除的时候记住不要带域名 也不要带  / 这个路径符号
     */
    function delfile($path_file) {
        if (config('config.OSS_OPEN')) {
            try {
                // 删除的时候记住不要带域名,也不要带'/'这个路径符号,如全路径是:'https://hxty.oss-cn-beijing.aliyuncs.com/data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf',则这里的path_file路径为:'data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf'。
                $path_file=trim(parse_url($path_file)['path'],'/');//去掉域名,
                $ossClient = new \OSS\OssClient(config('config.OSS_KEY'), config('config.OSS_SECRET'), config('config.OSS_ENDPOINT'));
                $ossClient->deleteObject(config('config.OSS_BUCKET'), $path_file);
            } catch (OssException $e) {
                printf(__FUNCTION__ . ": FAILED\n");
                printf($e->getMessage() . "\n");
                return;
            }
        } else {
            $val2 = "." . $path_file;
            if (file_exists($val2)) {
                $unlinkfilename = trim($path_file, "/");
                unlink($unlinkfilename);
            }
        }
    }

删除的时候记住不要带域名,也不要带'/'这个路径符号,如全路径是:'https://hxty.oss-cn-beijing.aliyuncs.com/data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf',则这里的path_file路径为:'data/pdf/contract/2021-06-16/HXSHB21061611594482340c.pdf'。

到此这篇关于php 文件上传至OSS及删除远程阿里云OSS文件的文章就介绍到这了,更多相关php删除阿里云OSS文件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/han_cui/article/details/118440422