【问题标题】:how to Add current file to "delete list" except file with mp3 and mp4 extension?如何将当前文件添加到“删除列表”中,除了带有 mp3 和 mp4 扩展名的文件?
【发布时间】:2017-11-10 10:36:58
【问题描述】:

我想创建删除列表,但 mp3 和 mp4 文件除外

我的代码是

错误 致命错误:在第 39 行的 E:\xampp\htdocs\tes\index.php 中调用未定义函数 endsWith()

    $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;
        }
    }
}

请大家帮帮我

【问题讨论】:

  • 检查文件的mime类型,文件扩展名(例如.mp3)不可靠
  • 它无法解决
  • 我注意到你已经指定了php-5.3 标签。请注意,这个 php 版本现在非常旧且不受支持。您应该紧急考虑升级到至少 5.6 版以避免安全问题。

标签: php php-5.3


【解决方案1】:

再次,检查文件扩展名是不可靠的。您应该检查文件的 MIME 类型:mime_content_type

在您的代码中,您可以执行以下操作:

// 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;
        }

但要小心,因为 mp3 有很多不同的 mime 类型,如您所见 here

编辑: 如果你不想检查多个 mime_types 你可以使用in_array:

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

【讨论】:

  • 即使检查 mime 类型也可能不可靠且危险,但至少比文件扩展名要好。
  • 如何添加两种类型??意思是音频/mp3 和视频/mp4 或更多
【解决方案2】:

没有endsWith 函数。您需要创建自己的。检查这个例子。

startsWith() and endsWith() functions in PHP

【讨论】:

  • 那我该怎么办?
  • 查看我评论中的链接有例子,如何实现这个功能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2014-04-19
相关资源
最近更新 更多