搜尋
首頁後端開發PHP問題怎麼解決php壓縮中文亂碼問題

怎麼解決php壓縮中文亂碼問題

Nov 14, 2022 am 10:25 AM
php中文亂碼

php壓縮中文亂碼的解決方法:1、透過「composer require nelexa/zip」安裝PhpZip;2、開啟「ZipFile.php」;3、找到「extractTo」方法,將「is_writable」函數改掉或去掉;4、轉換一下編碼格式即可。

怎麼解決php壓縮中文亂碼問題

本教學操作環境:windows7系統、PHP8.1版、Dell G3電腦。

怎麼解決php壓縮中文亂碼問題?

解決php使用ZipArchive解壓縮時中文亂碼問題(純php,繞開ZipArchive)

解決php使用ZipArchive解壓縮時中文亂碼問題

使用php自帶的ZipArchive來解壓縮帶中文文件名壓縮包時會造成亂碼,現像如下:

怎麼解決php壓縮中文亂碼問題

網上查閱基本上給出的答案大同小異,自己照著同樣的方法試了都無法解決,下圖是網路上給出的方案:

怎麼解決php壓縮中文亂碼問題

經過摸索終於找到了解決方案,那就是棄用ZipArchive,選擇其他的途徑,經過比較我選擇了“PhpZip”,優點是:純php(不需要擴充功能和類別),以下介紹安裝方法:

composer安裝:

composer require nelexa/zip

如果選擇版本的話:composer require nelexa /zip:^3.0 (3.0版本號)

使用方法:

//解压文件
$fileAddess = "压缩包地址";
$toDir = "解压目录";
$zipFile = new \PhpZip\ZipFile();
$zipFile->openFile($fileAddess) // open archive from file
->extractTo($toDir); // extract files to the specified directory
$zipFile->close();

註解:(**如果安裝好使用正常請忽略下面的註解內容**)

1.我的壓縮包放在共享盤裡面連接的是smb地址,可能會出現報錯:"Destination is not writable directory",這個坑已經踩過:打開ZipFile.php這個文件,找到“extractTo”方法,將「is_writable」函數改掉,或去掉,然後再轉換編碼格式! ,以下有註解的是我修改的內容!

/**
     * Extract the archive contents
     *
     * Extract the complete archive or the given files to the specified destination.
     *
     * @param string $destination Location where to extract the files.
     * @param array|string|null $entries The entries to extract. It accepts either
     *                                   a single entry name or an array of names.
     * @return ZipFile
     * @throws ZipException
     */
    public function extractTo($destination, $entries = null)
    {
        if (!file_exists($destination)) {
            throw new ZipException("Destination " . $destination . " not found");
        }
        if (!is_dir($destination)) {
            throw new ZipException("Destination is not directory");
        }
        $is_really_writable = $this->is_really_writable($destination);
        if (!$is_really_writable) {
            throw new ZipException("Destination is not writable directory");
        }
        /**
         * @var ZipEntry[] $zipEntries
         */
        if (!empty($entries)) {
            if (is_string($entries)) {
                $entries = (array)$entries;
            }
            if (is_array($entries)) {
                $entries = array_unique($entries);
                $flipEntries = array_flip($entries);
                $zipEntries = array_filter(
                    $this->centralDirectory->getEntries(),
                    function ($zipEntry) use ($flipEntries) {
                        /**
                         * @var ZipEntry $zipEntry
                         */
                        return isset($flipEntries[$zipEntry->getName()]);
                    }
                );
            }
        } else {
            $zipEntries = $this->centralDirectory->getEntries();
        }
        foreach ($zipEntries as $entry) {
            /******************************王天佑添加的逻辑start************************************/
            header("Content-type:text/html;charset=bgk");
            $entry_getName = iconv('GB2312', 'UTF-8//ignore',$entry->getName());
            //header("Content-type:text/html;charset=utf-8");
            /******************************王天佑添加的逻辑start************************************/
            $file = $destination . DIRECTORY_SEPARATOR . $entry_getName;
            if ($entry->isDirectory()) {
                if (!is_dir($file)) {
                    if (!mkdir($file, 0755, true)) {
                        throw new ZipException("Can not create dir " . $file);
                    }
                    chmod($file, 0755);
                    touch($file, $entry->getTime());
                }
                continue;
            }
            $dir = dirname($file);
            if (!is_dir($dir)) {
                if (!mkdir($dir, 0755, true)) {
                    throw new ZipException("Can not create dir " . $dir);
                }
                chmod($dir, 0755);
                touch($dir, $entry->getTime());
            }
            if (file_put_contents($file, $entry->getEntryContent()) === false) {
                throw new ZipException('Can not extract file ' . $entry_getName);
            }
            touch($file, $entry->getTime());
        }
        return $this;
    }
    //王天佑加的新逻辑,判断目录是否可写
    public function is_really_writable($file){
        if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE) {
            return is_writable($file);
        }
        if (is_dir($file)) {
            $file = rtrim($file, '/') . '/' . md5(mt_rand(1,100) . mt_rand(1,100));
            if (($fp = @fopen($file, "w+")) === FALSE) {
                return FALSE;
            }
            fclose($fp);
            @chmod($file, 0777);
            @unlink($file);
        } elseif (!is_file($file) OR ($fp = @fopen($file, "r+")) === FALSE) {
            fclose($fp);
            return FALSE;
        }
        return TRUE;
    }

推薦學習:《PHP影片教學

以上是怎麼解決php壓縮中文亂碼問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
酸與基本數據庫:差異和何時使用。酸與基本數據庫:差異和何時使用。Mar 26, 2025 pm 04:19 PM

本文比較了酸和基本數據庫模型,詳細介紹了它們的特徵和適當的用例。酸優先確定數據完整性和一致性,適合財務和電子商務應用程序,而基礎則側重於可用性和

PHP安全文件上傳:防止與文件相關的漏洞。PHP安全文件上傳:防止與文件相關的漏洞。Mar 26, 2025 pm 04:18 PM

本文討論了確保PHP文件上傳的確保,以防止諸如代碼注入之類的漏洞。它專注於文件類型驗證,安全存儲和錯誤處理以增強應用程序安全性。

PHP輸入驗證:最佳實踐。PHP輸入驗證:最佳實踐。Mar 26, 2025 pm 04:17 PM

文章討論了PHP輸入驗證以增強安全性的最佳實踐,重點是使用內置功能,白名單方法和服務器端驗證等技術。

PHP API率限制:實施策略。PHP API率限制:實施策略。Mar 26, 2025 pm 04:16 PM

本文討論了在PHP中實施API速率限制的策略,包括諸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之類的庫。它還涵蓋監視,動態調整速率限制和手

php密碼哈希:password_hash和password_verify。php密碼哈希:password_hash和password_verify。Mar 26, 2025 pm 04:15 PM

本文討論了使用password_hash和pyspasswify在PHP中使用密碼的好處。主要論點是,這些功能通過自動鹽,強大的哈希算法和SECH來增強密碼保護

OWASP前10 php:描述並減輕常見漏洞。OWASP前10 php:描述並減輕常見漏洞。Mar 26, 2025 pm 04:13 PM

本文討論了OWASP在PHP和緩解策略中的十大漏洞。關鍵問題包括注射,驗證損壞和XSS,並提供用於監視和保護PHP應用程序的推薦工具。

PHP XSS預防:如何預防XSS。PHP XSS預防:如何預防XSS。Mar 26, 2025 pm 04:12 PM

本文討論了防止PHP中XSS攻擊的策略,專注於輸入消毒,輸出編碼以及使用安全增強的庫和框架。

PHP接口與抽像類:何時使用。PHP接口與抽像類:何時使用。Mar 26, 2025 pm 04:11 PM

本文討論了PHP中接口和抽像類的使用,重點是何時使用。界面定義了無實施的合同,適用於無關類和多重繼承。摘要類提供常見功能

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具