PHP7 重命名迭代器内目录中的所有文件 (windows 10) 64 位 XAMPP

PHP7 Rename all files in directory within iterator (windows 10) 64-bit XAMPP

我需要使用以下 php 代码将所有歌曲重命名为整数(数字),但显示错误:

Warning: rename(abc.mp3,2.4): The system cannot find the file specified. (code: 2) in D:\xampp\htdocs\hta\file_renames.php on line 14  

command PATHINFO_EXTENSION 在这里也不起作用? 我正在使用 windows 10 和 xampp (php7)

<?php $total = 0;
$dir = "songs/";
foreach (new DirectoryIterator($dir) as $fileInfo) {
if(!$fileInfo->isDot()){
    $total +=1;
    $file = $fileInfo->getFilename();
    rename($file,$total.'.'.PATHINFO_EXTENSION);
}
}
echo('Total files: '.$total);
?>

如何将我所有的 .mp3 文件重命名为一个 number.mp3 文件?在循环内?

您需要提供 rename 的完整路径(可以是相对路径)。关于 PATHINFO_EXTENSION,你只是在误用它。这是固定代码:

<?php
$total = 0;
$dir = "songs/";
foreach (new DirectoryIterator($dir) as $fileInfo) {
    if(!$fileInfo->isDot()){
        $total +=1;
        $file = $dir.$fileInfo->getFilename();
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        $newFile = $dir.$total.'.'.$ext;
        rename($file, $newFile);
    }
}
echo('Total files: '.$total);
?>