您需要帮助来创建嵌套文件夹。
我目前有一个名为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;
}
,但未创建子文件夹。我该如何修改?
我将每周运行一次代码,我还想检查已创建的文件夹和未创建的文件夹。如果该文件夹不存在,则创建该文件夹。
答案 0 :(得分:0)
我将看一下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 Illuminate\Support\Facades\Storage;