E
E
Egor Mikheev2016-08-25 11:22:36
PHP
Egor Mikheev, 2016-08-25 11:22:36

How to solve the problem with Cyrillic when creating archives using php zip?

In particular, a utf-8 encoded database dump is added to the archive and it breaks at the exit from the archive.

$archiveName = date('YmdHi') . '-' . APP::$conf['location'][1] . '.zip';


    $zip = new ZipArchive;
    $zip->open($archiveName, ZIPARCHIVE::CREATE);

    // create recursive directory iterator
    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(ROOT), RecursiveIteratorIterator::LEAVES_ONLY);
    foreach ($files as $name => $file) {
        $filePath = $file->getRealPath();
        $localPath = explode(ROOT, $filePath);

        if ((strpos($filePath, ROOT . '/logs') !== false) || (strpos($filePath, ROOT . '/tmp') !== false)) {
            continue;
        } else {
            if (is_file($filePath)) {
             //   echo mb_substr($localPath[1], 1)."<br>";
                $zip->addFile(mb_substr($localPath[1],1));
            }
        }
    }
    $zip->close();

    header('Content-Type: application/zip; charset=utf-8');
    header('Content-Length: ' . filesize($archiveName));
    header('Content-Disposition: attachment; filename="' . $archiveName.'"');

    readfile($archiveName);
  //  unlink($archiveName);
    rmdir($backupDir);
    exit;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question