在這裏,你有辦法做到這一點:
<?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
你到目前爲止做了什麼? –