Я хочу переместить файлы из одного каталога в другой.Когда файл перемещается из исходного каталога, счетчик исходного каталога должен быть уменьшен на количество перемещаемых файлов.Когда файл перемещается в каталог назначения, счетчик файла назначения должен увеличиваться на количество перемещаемых файлов.
Я пробовал это с помощью функции movefile, но увеличивается только счетчик каталога назначения, а источник - нет.
Ниже приведена функция moveFile, которая передает параметр запроса:
<pre>
<code>
public function moveFiles(Request $request){
if($request->has('moveToID') && $request->has('IDs')) {
//return response()->json($request);
$moveID = $request->input('moveToID');
$IDs = $request->input('IDs');
$moveTO = Filemanager::find($moveID);
foreach ($IDs as $parentIndex => $ID) {
$parent = Filemanager::find($ID);
// If it's moving into the same directory , naughty user!
if($parent->path == '') {
if ($parent->real_name == $moveTO->real_name)
continue;
}
// if it's already in the same root directory
if($parent->path == $moveTO->real_name)
continue;
// First move the selected directory
$currentPath = 'file_manager/' . $parent->path . '/' .
$parent->real_name;
$newPath = 'file_manager/' . $moveTO->real_name . '/' .
$parent->real_name;
if (Storage::move($currentPath, $newPath)) { // If directory
moved update all of it's children path
if ($parent->type == 'dir') {
// Update moving directory's root sub files and
//folders count
$movingPathArray = explode('/',$parent->path);
foreach($movingPathArray as $movingPath){
$root =
Filemanager::where('real_name',$movingPath)->first();
if(!empty($root)) {
$root->sub_folders -= $parent->sub_folders +1;
$root->sub_files -= $parent->sub_files;
$root->save();
}
}
// If it's a directory and has children
$children = Filemanager::where('path', 'like', "%$parent->real_name%")->get();
if (!empty($children)) {
foreach ($children as $index => $child) {
// Remove the root directory from path
$path = $child->path;
$path_array = explode('/', $path);
foreach ($path_array as $key => $value) {
if ($value == $parent->real_name) {
$path_sliced[$index] = array_slice($path_array, $key);
continue;
}
}
if (!empty($path_sliced)) {
foreach ($path_sliced as $key => $value) {
$child_path[$index] = implode('/', $value);
$child_path[$index] = $moveTO->real_name . '/' . $child_path[$index];
}
if (!empty($child_path[$index])) {
$updated_children[$index] = Filemanager::find($child->id);
$updated_children[$index]->path = $child_path[$index];
$updated_children[$index]->save();
}
}
}
}
// Update the directories path itself
$parent->path = $moveTO->real_name;
$result = $parent->save();
// Gather count of sub files and directories of moving elements
$moveFilesCount[$parentIndex]['files'] = $parent->sub_files;
$moveFilesCount[$parentIndex]['dirs'] = $parent->sub_folders + 1;
} else {
// It's a file
// Update source root directories sub folders and sub files count
if(!empty($movingPathArray)) {
$movingPathArray = explode('/', $parent->path);
foreach ($movingPathArray as $movingPath) {
$root = Filemanager::where('real_name', $movingPath)->first();
$root->sub_files -= 1;
$root->save();
}
}
// Update destination folder's sub folders and sub files count
$parent->path = $moveTO->real_name;
$result = $parent->save();
$moveFilesCount[$parentIndex]['files'] = 1;//Destination file is incremented here
$moveFilesCount[$parentIndex]['dirs'] = 0;
}
} else {
return '????? ????? ???? , ???? ???? ??? ????';
}
}
//Update 'Move TO' directory sub files and folders count
$movePath = 'file_manager/' . $moveTO->path . '/' . $moveTO->real_name;
$sub_files = 0;
$sub_folders = 0;
if(!empty($moveFilesCount)) {
foreach ($moveFilesCount as $item) {
$sub_files += $item['files'];
$sub_folders += $item['dirs'];
}
}
$moveTO->sub_files = $moveTO->sub_files + $sub_files;
$moveTO->sub_folders = $moveTO->sub_folders + $sub_folders;
$moveTO->save();
}
else{
return '???? ?????? ??? ???? ??? ?? ?? ????? ???? ?????? ???? ????';
}
return 'success';
}