storage_path('app/public'), ]); foreach ($links as $link => $target) { if (is_link($link)) { $this->line("The [{$link}] link already exists. Skipping."); continue; } if (is_dir($link)) { $this->warn("A directory already exists at [{$link}]. Removing it before creating the symlink."); $this->removeDirectory($link); } if (file_exists($link)) { $this->warn("A file already exists at [{$link}]. Removing it before creating the symlink."); unlink($link); } if (!is_dir($target)) { mkdir($target, 0755, true); } symlink($target, $link); $this->info("The [{$link}] link has been connected to [{$target}]."); } $this->info('Storage links created successfully.'); return self::SUCCESS; } /** * Recursively remove a directory and its contents. */ protected function removeDirectory(string $dir): void { if (!is_dir($dir)) { return; } $items = array_diff(scandir($dir), ['.', '..']); foreach ($items as $item) { $path = $dir . DIRECTORY_SEPARATOR . $item; if (is_dir($path) && !is_link($path)) { $this->removeDirectory($path); } else { unlink($path); } } rmdir($dir); } }