aliyun_oss文件夹内的文件上传

来源:互联网 发布:格伦罗宾逊数据 编辑:程序博客网 时间:2024/06/01 08:02

$path----本地需要上传的路径

$scanFiles---记录被扫描的文件名,初次进入可传空数组

function traverse($path = '.',&$scanFiles) {

$current_dir = opendir($path);    //opendir()返回一个目录句柄,失败返回false
while(($file = readdir($current_dir)) !== false) {    //readdir()返回打开目录句柄中的一个条目
$sub_dir = $path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径
if($file == '.' || $file == '..') {
continue;
} else if(is_dir($sub_dir)) {    //如果是目录,进行递归
//echo 'Directory ' . $file . ':<br>';
traverse($sub_dir,$scanFiles);
} else {    //如果是文件,直接输出
if($file != 'Thumbs.db' ){
$scanFiles[] = $path . DIRECTORY_SEPARATOR . $file ;

}
}
}

}


最后返回$scanFiles

foreach($scanFiles as $key => $aFile){
// 先把本地的example.jpg上传到指定$bucket, 命名为$object
$ossClient->uploadFile(BUCKET_NAME,"www.meidouchina.com/".str_replace('\\','/',$aFile),$aFile);
}

做个循环上传就ok了

$ossClient->uploadFile(aliyun_oss的存储空间名称 , 需要放到阿里云oss上的路径,需上传的本地文件路径 )





以下是我php的demo,大家仅查看对自己有用的就行了,没用的直接忽略,我把这个丢上来仅仅是为了我之后能偷懒而已

error_reporting(E_ALL);
define('CURSCRIPT', 'index');
require("libraries/common.inc.php");
require("share.inc.php");
//render("member");


//自定义
//OSS 容器名
define('BUCKET_NAME', 'meidou');
//指定遍历目录
define('OBJ_DIR', '.'.DIRECTORY_SEPARATOR.'attachment'.DIRECTORY_SEPARATOR.'video');


//用目录及文件名组成的路径数组
$scanFiles = array();

require_once __DIR__ . '/libraries/aliyun-oss-php-sdk/samples/Common.php';
//require_once './src/OSS/OssClient.php';
use OSS\OssClient;
$ossClient = Common::getOssClient();
if (is_null($ossClient)) die(1);
//////


echo 2; 


traverse('static', $scanFiles);
//print_r($scanFiles);die;
print_r(' '.count($scanFiles).' ');


foreach($scanFiles as $key => $aFile){
// 先把本地的example.jpg上传到指定$bucket, 命名为$object
$ossClient->uploadFile(BUCKET_NAME,"www.meidouchina.com/".str_replace('\\','/',$aFile),$aFile); 
}


echo ' upload success! ';


/* 次处是单个文件夹上传的,对于文件夹里面包含文件夹的,此处不适用

$localDirectory = "static";
$prefix = "www.meidouchina.com/static";
$bucket = 'meidou';
$ossClient->uploadDir($bucket, $prefix, $localDirectory);

 */


function traverse($path = '.',&$scanFiles) {
$current_dir = opendir($path);    //opendir()返回一个目录句柄,失败返回false
while(($file = readdir($current_dir)) !== false) {    //readdir()返回打开目录句柄中的一个条目
$sub_dir = $path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径
if($file == '.' || $file == '..') {
continue;
} else if(is_dir($sub_dir)) {    //如果是目录,进行递归
//echo 'Directory ' . $file . ':<br>';
traverse($sub_dir,$scanFiles);
} else {    //如果是文件,直接输出
if($file != 'Thumbs.db' ){
$scanFiles[] = $path . DIRECTORY_SEPARATOR . $file ;

}
}
}
}