欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现批量下载百度云盘文件例子分享

程序员文章站 2023-11-20 10:22:22
百度开放云phpsdk下载地址:http://developer.baidu.com/wiki/index.php?title=%e5%b8%ae%e5%8a%a9%e6%...

百度开放云phpsdk下载地址:http://developer.baidu.com/wiki/index.php?title=%e5%b8%ae%e5%8a%a9%e6%96%87%e6%a1%a3%e9%a6%96%e9%a1%b5/web%e5%ba%94%e7%94%a8%e6%8e%a5%e5%85%a5/sdk

实例代码:

复制代码 代码如下:

<?php
/**
 * author: hankcs
 * date  : 14-3-29
 * time  : 下午10:29
 */
ini_set("max_execution_time", 36000);
require_once '../bcs.class.php';
require_once '../krumo/class.krumo.php';
$host = 'bcs.duapp.com';
$ak = '你的公钥';
$sk = '你的私钥';
$bucket = '要下载的bucket名称';
$baidu_bcs = new baidubcs ( $ak, $sk, $host );

$response = json_decode($baidu_bcs->list_object ( $bucket, array('start' => 0,'limit' => 1024,) )->body);
$downloaded_size = 0;
while($downloaded_size < $response->object_total)
{
    krumo($response);
    // 下载它们
    foreach($response->object_list as $object)
    {
        if(!$object->is_dir)
        {
            mk_dir('.'.$object->parent_dir);
            $result = $baidu_bcs->get_object ( $bucket, $object->object, array ("filewriteto" => '.'.$object->object ) );
        }
    }
    $downloaded_size += count($response->object_list);
    $response = json_decode($baidu_bcs->list_object ( $bucket, array('start' => $downloaded_size,'limit' => 1024,) )->body);
}

function mk_dir($dir, $mode = 0755)
{
    if (is_dir($dir) || @mkdir($dir,$mode)) return true;
    if (!mk_dir(dirname($dir),$mode)) return false;
    return @mkdir($dir,$mode);
}