PHP ZipArchive dont support UTF8 files for open

2019-03-01 11:51发布

PHP ZipArchive dont support UTF8 files for open

my problem is OPEN files with UTF8 name. ZipArchive dont open files with UTF8 character. i dont add new file i need only open file.

php: 5.6 and Use Yii2.

code:

$path = "files/تست تست.zip";
        $zip = new \ZipArchive();
        if($zip->open($path) === true) {

            return "File opened";
        }
        else
        {
            return "File could not be opened";
        }

1条回答
老娘就宠你
2楼-- · 2019-03-01 12:47

Sorry about marking this as a duplicate for an unrelated issue.

I'm able to open UTF-8 zip files without a problem using PHP 5.6.

This code will create a new ZIP file with that filename without a problem, with a "test.txt" file in it:

$path = "تست تست.zip";
$zip = new ZipArchive();

if($zip->open($path, ZipArchive::CREATE) === true) {
    echo "File opened\n";
    $zip->addFromString("test.txt", "Test file");
    $zip->close();
} else {
    echo "File could not be opened";
}

This code will open an existing ZIP file with that name and print out the first filename from within the archive:

$path = "تست تست.zip";
$zip2 = new ZipArchive();

if($zip2->open($path) === true) {
    echo "File opened\n";
    echo $zip2->getNameIndex(0);
    $zip2->close();
} else {
    echo "File could not be opened";
}

These examples work fine in the PHP Sandbox and on phptester.com (no direct link available). I tried it on 3v4l.org as well, but they don't have the php-zip extension enabled so ZipArchive is not available there.

查看更多
登录 后发表回答