清空又拍云服务上指定目录下内容相关脚本

公共部分:

<?php
/**
 * User: xahy
 * Description: 公共部分
 */
require __DIR__.'/vendor/autoload.php';

use Upyun\Upyun as UpYunObj;
use Upyun\Config as UpYunConfig;

/****
 * 链接又拍云服务
 * @param string $serverName 服务名称
 * @param string $serverUser 用户
 * @param string $serverPwd  密码
 */
$serverName = '';
$serverUser = '';
$serverPwd = '';
$serviceConfig = new UpYunConfig($serverName, $serverUser, $serverPwd);
$client = new UpYunObj($serviceConfig);

标题清空内容脚本:

<?php
/**
 * User: xahy
 * Description: 清空指定目录下所有文件和目录,默认根目录
 */

require_once __DIR__."/in.php";

/***
 * 当指定目录存储使用量比较大的时候,请打开以下注释取消运行时间限制和声明后台运行。
 * 如果想在linux上持续执行,请使用命令: nohup php -f /home/wwwroot/del/loo.php &
 */

//后台运行
//ignore_user_abort();
//取消脚本运行时间的超时上限
//set_time_limit(0);



///要删除的目录
$path = "/";


echo "<pre>";

try{
    if($client->getMimetype($path) != "application/octet-stream"){
        var_dump("rm message:请填写正确的目录");
        die;
    }

    while(! empty($client->read($path)["files"])){
        c($client,$path);
    }

    var_dump("rm message:success");

}catch(\Exception $e){
    var_dump($e->getMessage());
    die;
}







/****
 * User: xahy
 * Description: 递归删除目录or文件
 * @param $client
 * @param string $path
 * @param string $f
 */
function c($client,$path = "/",$f = "F"){
    $res = $client->read($path);
    $files = $res["files"];

    ///如果读取内容为空且是目录,则删除此目录
    if(empty($files) && $f == "F" ){
        $client->delete($path,true);
    }else{
    ///遍历读取出来的内容,如果是文件则删除当前文件,如果是目录则遍历此目录下内容
        foreach($files as $k=>$v){
            $v_type = $v["type"];
            $v_name = $v["name"];
            $actionPath = substr($path, -1) == "/" ? $path.$v_name : $path."/".$v_name;

            if($v_type == "N"){
                $client->delete($actionPath,true);
            }else if($v_type == "F"){
                c($client,$actionPath,$v_type);
            }
        }
    }

}

查看清理状态脚本:

<?php
/**
 * User: xahy
 * Description: 查看使用量,默认根目录
 */
require_once __DIR__."/in.php";

echo "<pre>";

$files = $client->read("/")['files'];

if(empty($files)){
    var_dump("check result:清理已完成");
}else{
    var_dump("清理进行中....");
    var_dump("整个云服务使用空间大小,参考值:" . BytesTo($client->usage("/")));
}

完整代码 gitee 地址:https://gitee.com/xianhenyuan/rm-up-content

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值