1

i want to make delete-list but except mp3 and mp4 file

my code is

error Fatal error: Call to undefined function endsWith() in E:\xampp\htdocs\tes\index.php on line 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;
        }
    }
}

anyone help me please

2 Answers2

1

Again, checking the file-extension is unreliable. You should check the mime-type of the file with: mime_content_type.

In your code, you can do something like this:

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

But be carefull because there're a lot of different mime-types for mp3, as you can see here.

edit: If you wan't to check for multiple mime_types you can use in_array:

if(in_array(mime_content_type($filePath), ['audio/mp3','audio/mpeg']))
Sepultura
  • 997
  • 1
  • 9
  • 28
0

The is no endsWith function. You need to create your own. Check this example.

startsWith() and endsWith() functions in PHP