清空又拍雲服務上指定目錄下內容相關腳本

公共部分:

<?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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章