写的很简单也不严密,有时间完善下~
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;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。