zoukankan      html  css  js  c++  java
  • 又拍云递归删除目录及文件

    偶然翻看了一下又拍云存储的api文档,发现无法删除非空目录,简单写了一个,使用Yii框架,所以可能看起来有点怪,见谅吧。

     1 <?php
     2 
     3 /**
     4  * 文件说明.
     5  * 
     6  * @author Kun Xu <kunx@jumei.com>
     7  */
     8 class UpyunController extends Controller
     9 {
    10 
    11     /**
    12      * 删除文件夹及文件,递归删除目录及文件.
    13      * @param string $path 要递归删除的目录.
    14      * @param string $bucketname 空间名.
    15      */
    16     public function actionRemoveDirQuiet($path = '/', $bucketname = '')
    17     {
    18         $bucketname = '<bucketname>'; //空间名.
    19         $username = '<username>'; //操作者账号.
    20         $password = '<password>'; //操作者密码.
    21         Yii::import('ext.UpYun'); //Yii引入UpYun SDK,记得将upyun.class.php改名为UpYun.php放入extensions目录.
    22         $upYun = new UpYun($bucketname, $username, $password);
    23         if (strncasecmp('/', $path, 1) !== 0) {
    24             $path = '/' . $path;
    25         }
    26         $this->removeDir($path, $upYun); //调用删除方法.
    27     }
    28 
    29     /**
    30      * 删除目录.
    31      * @param string $path  要删除的目录路径.
    32      * @param UpYun  $upYun UpYun实例.
    33      */
    34     private function removeDir($path, UpYun $upYun)
    35     {
    36         $list = $upYun->getList($path); //获取目录列表信息.
    37         if ($list) {
    38             foreach ($list as $item) {
    39                 if (strrpos($path, '/') == strlen($path) - 1) {//判断路径是否以/结束,由于开始路径可能是以/结束的,所以这里需要排除一下
    40                     $file = $path . $item['name'];
    41                 } else {
    42                     $file = $path . '/' . $item['name'];
    43                 }
    44                 if ($item['type'] == 'folder') {//是文件夹,递归删除子文件夹文件.
    45                     $this->removeDir($file, $upYun);
    46                 } else {//普通文件,直接删除
    47                     $upYun->deleteFile($file);
    48                 }
    49             }
    50         }
    51         $upYun->rmDir($path);
    52     }
    53 
    54 }
  • 相关阅读:
    LeetCode 264. Ugly Number II
    LeetCode 231. Power of Two
    LeetCode 263. Ugly Number
    LeetCode 136. Single Number
    LeetCode 69. Sqrt(x)
    LeetCode 66. Plus One
    LeetCode 70. Climbing Stairs
    LeetCode 628. Maximum Product of Three Numbers
    Leetcode 13. Roman to Integer
    大二暑假周进度报告03
  • 原文地址:https://www.cnblogs.com/quinnxu/p/3778936.html
Copyright © 2011-2022 走看看