簡體   English   中英

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  

命令PATHINFO_EXTENSION在這里也不起作用? 我正在使用帶有xampp的Windows 10(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);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM