在这里,你有办法做到这一点:
<?php
// Array with all subdirectories in directory
$dirArray = array_filter(glob('/path/to/directory/*'), 'is_dir');
// $dir is the path to the subdirectory
foreach ($dirArray as $dir) {
// $dirName has the future name of files in that subdirectory
$dirName = basename($dir);
// Take all the elements in the subdirectory (except '.' and '..')
$filesArray = array_diff(scandir($dir), array('.', '..'));
$i = 0;
foreach ($filesArray as $file) {
// Take the file extension for the rename method
$fileExtension = pathinfo($file, PATHINFO_EXTENSION);
$oldName = $dir . "/" . $file;
$newName = $dir . "/" . $dirName . "-" . $i . "." . $fileExtension;
rename($oldName, $newName);
$i++;
}
}
如果你确信你将不得不在每个子目录只有一个文件,你可以使用此行来$newName
:
$newName = $dir . "/" . $dirName . "." . $fileExtension;
这是不一个问题:S ... – Asfo
你到目前为止做了什么? –