开发者社区> 问答> 正文

分享一个php 批量上传文件夹的方法

写的很简单也不严密,有时间完善下~

    public function upload_directory(){

        $files = $this->get_filetree('./lib');////自己替换./lib为想要上传的文件夹
        //dump($files);exit;
        $has_sub_directory = false;
        $prefix = 'xxxxxx';//自己替换
        $bucket = 'xxx';//自己替换
        foreach($files as $file){
            $object = $has_sub_directory == true ? substr ( $file, strlen ( $dir ) ) : "/" . basename ( $file );
            $this->create_object_by_content($bucket,$prefix . $object,array('file'=>$file));
        }

        //dump($objects);
    }



    protected function create_object_by_content($bucket,$object,$options){

        $length = 0;
        $content = '';
        $fp = fopen($options['file'],'r');
        if($fp){
            $f = fstat($fp);
            $length = $f['size'];
            while(!feof($fp)){
                $content .= fgets($fp,8192);
            }
        }

        $opts = array(
            'content' => $content,
            'length' => strlen($content),
            'headers'=>array(
                'Cache-Control'=>'max-age=2592000',
            ),
        );

        $r = $this->oss->upload_file_by_content($bucket,$object,$opts);

        echo $options['file'].'-------------'.($r->isOK()?"<font color=green>success</font>":"<font color=red>failed</font>").'<br/>';
    }

文件树获取

    public function get_filetree($dir, $file_prefix = "/*") {
        $tree = array ();
        foreach ( glob ( $dir . $file_prefix ) as $single ) {
            if (is_dir ( $single )) {
                $tree = array_merge ( $tree, $this->get_filetree ( $single ) );
            } else {
                $tree [] = $single;
            }
        }
        return $tree;
    }


展开
收起
晨暮随心 2012-02-24 11:18:28 17402 0
9 条回答
写回答
取消 提交回答
  • 多谢楼主分享哈。
    2012-08-07 14:41:03
    赞同 展开评论 打赏
  • Re分享一个php批量上传文件夹的方法
    刚刚用了不错
    2012-08-02 15:58:27
    赞同 展开评论 打赏
  • Re分享一个php批量上传文件夹的方法
    不错,用上了
    2012-07-31 16:32:48
    赞同 展开评论 打赏
  • Re分享一个php批量上传文件夹的方法
    真不错,多谢分享的精神……
    2012-07-13 14:24:37
    赞同 展开评论 打赏
  • Re分享一个php批量上传文件夹的方法
    oss不会处理 Objcet
    2012-06-21 15:48:29
    赞同 展开评论 打赏
  • Re分享一个php批量上传文件夹的方法
    能不能说一下用法,php放在本地的web服务器上,还是放在oss里面?oss里面能运行php吗?
    2012-06-18 14:07:50
    赞同 展开评论 打赏
  • 感谢分享,基础知识还是很重要的

    -------------------------

    写得好,感谢楼主分享

    2012-06-07 19:19:22
    赞同 展开评论 打赏
  • Re分享一个php批量上传文件夹的方法
    谢谢分享
    2012-05-25 10:50:30
    赞同 展开评论 打赏
  • 2012-03-25 22:14:53
    赞同 展开评论 打赏
滑动查看更多
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-深入浅出网络编程与swoole内核-吴镇宇 立即下载
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载