Below code generate a csv file that provide you name of all files exists in the folder.
<?php
function getAllFolders($path) {
// Check if the path is a directory
if (!is_dir($path)) {
return "The specified path is not a directory.";
}
// Scan the directory
$folders = [];
$items = scandir($path);
// Filter out the folders
foreach ($items as $item) {
if ($item != '.' && $item != '..') { // Skip the current and parent directory
if (is_dir($path . DIRECTORY_SEPARATOR . $item)) {
$folders[] = $item; // Add folder name to the array
}
}
}
return $folders;
}
function saveFoldersToCSV($folders, $csvFilePath) {
// Open the CSV file for writing
$file = fopen($csvFilePath, 'w');
// Write the folder names to the CSV file
foreach ($folders as $folder) {
fputcsv($file, [$folder]);
}
// Close the file
fclose($file);
}
// Specify the path to the directory and the CSV file
$path = 'Paste file path here'; // Change this to your desired path
$csvFilePath = 'folders_list.csv'; // Name of the CSV file
$folders = getAllFolders($path);
if (is_array($folders) && !empty($folders)) {
saveFoldersToCSV($folders, $csvFilePath);
echo "Folders have been saved to '$csvFilePath'.\n";
} else {
echo $folders ?: "No folders found in the specified directory.\n";
}
?>
No comments:
Post a Comment