如何将当前文件添加到除MP3和MP4扩展名的文件之外"delete list"?



我想制作删除列表,但除了mp3和mp4文件

我的代码是

错误致命错误:e: xampp htdocs tes tes index.php在第39行39

    $files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);
        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
        $zip->close();
        // Add current file to "delete list"
        // delete it later cause ZipArchive create archive only after calling close function and ZipArchive lock files until archive created)
        if ($file->getFilename().endsWith(".mp3")) error at this code
        {
            $filesToDelete[] = $filePath;
        }
    }
}

任何人帮助我

再次,检查文件扩展是不可靠的。您应该使用以下方式检查文件的哑光型。

在您的代码中,您可以做这样的事情:

// Add current file to "delete list"
        // delete it later cause ZipArchive create archive only after calling close function and ZipArchive lock files until archive created)
        if (mime_content_type($filePath) == "audio/mp3"))
        {
            $filesToDelete[] = $filePath;
        }

但是要小心,因为您可以在这里看到很多不同的mime类型。

编辑:如果您不愿检查多个mime_types,可以使用in_array:

if(in_array(mime_content_type($filePath), ['audio/mp3','audio/mpeg']))

无端函数。您需要创建自己的。检查此示例。

startswith((和php

中的endswith((函数

最新更新