将文件放在子文件夹中到Google驱动器中使用laravel

时间:2019-01-16 01:53:12

标签: php laravel-5

当我使用软件包nao-pon/flysystem-google-drive时,我在将文件上传到google驱动器的代码中遇到问题,此代码将被创建为text.txt到subdirectory =“ Contract”,但是我有许多名称不同的文件夹,它们是相同的2个文件夹子级是:合同,项目。我找不到一种方法将text.txt创建到我想要的文件夹名称中

我试图在

中修复查询
$dir = $contents->where('type', '=', 'dir')
    ->where('filename', '=', 'Contract')
    ->first();

但不起作用。

    Route::get('put-in-dir', function() {
$dir = '/';
$recursive = true; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));

$dir = $contents->where('type', '=', 'dir')
    ->where('filename', '=', 'Contract')
    ->first(); // There could be duplicate directory names!

if ( ! $dir) {
    return 'Directory does not exist!';
}

Storage::cloud()->put($dir['path'].'/test.txt', 'Hello World');

return 'File was created in the sub directory in Google Drive';
});

我想将text.txt创建到我想要的文本中。例如:我要在Yasuo的合同中创建。.

Shaco-
  --Contract
  --Project
Yasuo-
  --Contract
  --Project

1 个答案:

答案 0 :(得分:-1)

我找到了解决方案。这是我的解决方案:

Route::get('put-in-dir', function() {
$content = collect(Storage::cloud()->listContents('/', false));
    foreach ($content as $key => $value) {
        if($value['name'] == 'MrTrung')
            $root = $value['path'];
    }
    //dd($root);
$dir = '/'.$root;
$recursive = true; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));

$dir = $contents->where('type', '=', 'dir')
    ->where('filename', '=', 'Contract')
    ->first(); // There could be duplicate directory names!
if ( ! $dir) {
    return 'Directory does not exist!';
}
Storage::cloud()->put($dir['path'].'/test.txt', 'Hello World');
return 'File was created in the sub directory in Google Drive';
});

注意:安装Google Drive API v3时,一切都是正确的。我按照这种方式:https://gist.github.com/ivanvermeyen/cc7c59c185daad9d4e7cb8c661d7b89b

祝你好运。