【发布时间】:2018-11-23 15:50:15
【问题描述】:
您好,在创建嵌套文件夹方面需要帮助。
我目前有一个名为 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;
}
但它没有创建子文件夹。怎么修改?
我每周都会运行代码,我还想检查哪些文件夹已创建,哪些尚未创建。如果该文件夹不存在,则创建该文件夹。
【问题讨论】:
-
mkdir的第三个参数是“递归”,因此它将创建您尝试创建的文件夹的所有父文件夹。那不工作吗?另外,为什么在mkdir前面使用@?你在压抑什么?