This is a simplified PHP script that removes spaces from filenames in a specified folder and its subdirectories:
<?php
// www.websitefunctions.com
function removeSpacesFromFilenames($folderPath) {
if (is_dir($folderPath)) {
$files = scandir($folderPath);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$fullPath = $folderPath . '/' . $file;
if (is_dir($fullPath)) {
// Recursively process subdirectories
removeSpacesFromFilenames($fullPath);
} elseif (strpos($file, ' ') !== false) {
// Check if the filename contains spaces
$newFileName = str_replace(' ', '', $file);
// Construct the full paths for the old and new files
$oldFilePath = $folderPath . '/' . $file;
$newFilePath = $folderPath . '/' . $newFileName;
// Rename the file
if (rename($oldFilePath, $newFilePath)) {
echo "Renamed: $oldFilePath to $newFileName<br>";
} else {
echo "Error renaming: $oldFilePath<br>";
}
}
}
}
}
}
$folderToRename = '/path/to/your/folder/';
removeSpacesFromFilenames($folderToRename);
?>
The script above will remove spaces from filenames in the specified folder and its subdirectories. Just replace /path/to/your/folder/ with the actual path to the folder containing the files you want to process.
You can run this script by accessing it in your web browser. For example, if the script is located at http://yourdomain.com/remove_spaces.php, you can visit it in your browser, and it will start removing spaces from filenames. Be sure to back up your files or work on a copy before running the script to avoid data loss.