如何循环浏览文件夹和子文件夹并使用Laravel创建



Hi在创建嵌套文件夹时需要帮助。

我目前有一个名为images的文件夹,我想克隆这些文件夹并将其保存在不同的文件夹调用备份中。并且只有当用户单击备份时才会进行备份。

例如:

- Images
- Car
- A
- A1
- A1-1
- A2
- B
- Van

我该如何编写代码以便创建文件夹?

目前我已经这样做了,那么我该怎么做呢?

public function sync(Request $request)
{
$arr = [];
$folderToSync = $request->input('folderName');
$originalPath = public_path($folderToSync);
$newFolderPath = public_path('S3/'.$folderToSync);
$this->createFolder($newFolderPath); // create the selected folder
$directories = $this->getAllFolders($originalPath); // getting all folders under the original path
$this->folder($directories, $newFolderPath, $originalPath);
dd('end');
}
public function createFolder($path)
{
if (!is_dir($path)) {
@mkdir($path, 0777, true);
}
}
public function folder($directories, $newFolderPath, $originalPath)
{
foreach ($directories as $directory) {
$newPath = $newFolderPath.'/'.$directory;
$oriPath = $originalPath.'/'.$directory;
$this->createFolder($newPath);
$subFolders = $this->getAllFolders($oriPath);
if ($subFolders) {
$this->subfolder($subFolders, $newPath);
}
}
}
public function subfolder($directories, $path)
{
foreach ($directories as $directory) {
$this->createFolder($path.'/'.$directory);
}
}
public function getAllFolders($path)
{
return array_map('basename', File::directories($path));
}
public function getAllFiles($path)
{
return;
}

但它不创建子文件夹。我如何修改它?

我每周都会运行代码,我还想检查哪些文件夹已经创建,哪些还没有创建。如果文件夹不存在,则创建文件夹。

我想看看Laravel文档中的存储api:https://laravel.com/docs/5.7/filesystem#directories

获取文件夹和子文件夹:

// Recursive...
$directories = Storage::allDirectories($directory);

创建新目录:

Storage::makeDirectory($directory);

存储文件:

Storage::put('file.jpg', $contents);
Storage::put('file.jpg', $resource);

put方法将获取文件内容或资源。

不要忘记包括Storage立面:

use IlluminateSupportFacadesStorage;

最新更新