<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP zip &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/php-zip/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Fri, 16 Dec 2022 13:19:03 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>PHP zip &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>無法開啟 PHP ZipArchive 下載的 zip 檔</title>
		<link>https://noter.tw/10668/%e7%84%a1%e6%b3%95%e9%96%8b%e5%95%9f-php-ziparchive-%e4%b8%8b%e8%bc%89%e7%9a%84-zip-%e6%aa%94/</link>
					<comments>https://noter.tw/10668/%e7%84%a1%e6%b3%95%e9%96%8b%e5%95%9f-php-ziparchive-%e4%b8%8b%e8%bc%89%e7%9a%84-zip-%e6%aa%94/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Fri, 16 Dec 2022 13:19:02 +0000</pubDate>
				<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[網頁前端]]></category>
		<category><![CDATA[技術相關]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ZipArchive]]></category>
		<category><![CDATA[PHP zip]]></category>
		<category><![CDATA[PHP 壓縮檔案]]></category>
		<category><![CDATA[ob_clean()]]></category>
		<category><![CDATA[網頁直接下載檔案]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=10668</guid>

					<description><![CDATA[<p>PHP 內有一個用來操控壓縮檔的 ZipArchive 模組，今天收到問題回報說下載的 zip 檔沒辦法開啟，當下直覺認為是對方操作錯誤，給幾個同事測試之後，有一個同事同樣無法開啟，竟然是因為檔頭多了一些空白造成，這篇記錄此問題的解法。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/10668/%e7%84%a1%e6%b3%95%e9%96%8b%e5%95%9f-php-ziparchive-%e4%b8%8b%e8%bc%89%e7%9a%84-zip-%e6%aa%94/" data-wpel-link="internal">無法開啟 PHP ZipArchive 下載的 zip 檔</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>PHP 內有一個用來操控壓縮檔的 ZipArchive 模組，今天收到問題回報說下載的 zip 檔沒辦法開啟，當下直覺認為是對方操作錯誤，給幾個同事測試之後，有一個同事同樣無法開啟，竟然是因為檔頭多了一些空白造成，這篇記錄此問題的解法。</p>



<span id="more-10668"></span>



<h2 class="para wp-block-heading">PHP 壓縮檔案</h2>



<p>這邊是留給自己的 ZipArchive 用法的 memo，確定載入此模組後，只要使用以下方法就可以輕鬆把檔案壓縮成 zip</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// 建立 ZipArchive 物件
$zip = new ZipArchive();
// 在指定路徑中建立一個 zip 檔
$res = $zip->open('tmp_callback/123.zip', ZipArchive::CREATE);
// 把 tmp_callback/file1 新增至 zip 內，並改名為 00000001.txt
$zip->addFile('tmp_callback/file1', '00000001.txt');
// 關閉 zip 物件
$zip->close();</pre>



<h2 class="para wp-block-heading">透過網頁下載 zip 檔</h2>



<p>透過以下的語法可以讓瀏覽器直接下載 zip 檔，一方面在之前做權限控管以及避免洩漏檔案真實路徑</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$path = "/var/www/html/uploads/";
$filename = '03082358.zip';
	
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($path . $filename));
@readfile($file);</pre>



<h2 class="para wp-block-heading">無法開啟壓縮檔的原因</h2>



<p>直到有使用者反應壓縮檔無法開啟，透過 <a href="https://unzip-online.com/en/zip" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">unzip-online.com<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 線上解壓縮也失敗</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img fetchpriority="high" decoding="async" src="https://lh3.googleusercontent.com/pw/AL9nZEUmC2qlg12i3weQMfhMjRT7Fs36NTNVuXgAmZ14RgARuzc-f3A5P6-R6RVcbFWjuV5zg2buO7UDMNQzmuJq7Yjz6p6uhzi36eDw9VLmBYSYsFfAzZnhLX-h94Sj4Ha-4Vu11rgEEmMHazPjEb01fLom=w940-h463-no?authuser=1" alt="AL9nZEUmC2qlg12i3weQMfhMjRT7Fs36NTNVuXgAmZ14RgARuzc f3A5P6 R6RVcbFWjuV5zg2buO7UDMNQzmuJq7Yjz6p6uhzi36eDw9VLmBYSYsFfAzZnhLX h94Sj4Ha 4Vu11rgEEmMHazPjEb01fLom=w940 h463 no?authuser=1 無法開啟 PHP ZipArchive 下載的 zip 檔" width="470" height="232" title="無法開啟 PHP ZipArchive 下載的 zip 檔"></figure></div>


<p>使用 notepad++ 開啟後才發現 zip 檔開頭有三行空白，可是有些電腦可以正常開啟有些電腦卻不行</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img decoding="async" src="https://lh3.googleusercontent.com/pw/AL9nZEVkfKvVTFLvsECju82DL81lA0cgTr9UbDAzMFQKa0EYoBTd7BkCwQVNsgf7pAeJbFT5wvfG7ovzLSIsHevoDTCTe6caIDyIJmvsoi6cgQzor9JIL0mvkwUAsczIPFE7Eq6i99yno-QHzmq8WTTKaliH=w208-h119-no?authuser=1" alt="AL9nZEVkfKvVTFLvsECju82DL81lA0cgTr9UbDAzMFQKa0EYoBTd7BkCwQVNsgf7pAeJbFT5wvfG7ovzLSIsHevoDTCTe6caIDyIJmvsoi6cgQzor9JIL0mvkwUAsczIPFE7Eq6i99yno QHzmq8WTTKaliH=w208 h119 no?authuser=1 無法開啟 PHP ZipArchive 下載的 zip 檔" width="156" height="89" title="無法開啟 PHP ZipArchive 下載的 zip 檔"></figure></div>


<p>只要把那三行空白刪除，就可以正常解壓縮了 &#8230;</p>



<h2 class="para wp-block-heading">無法開啟壓縮檔的解決方法</h2>



<p>一開始以為是 ZipArchive 模組的問題，做了各種嘗試後發現，產出來的 zip 是正常的，並沒有上面那三行空白，那問題就是出在下載這段了。</p>



<p>試了很多方法發現只要有<code>&lt;?php include_once "xxx.php" ?&gt;</code>就會有三行空白的狀況，一開始笨笨的試著把所有用到的 include 都拔進來放，後來覺得這樣實在是又累又蠢，最後終於找到簡單解法 &#8230; 只要在<code>readfile</code>之前呼叫<code>ob_clean();</code>就可以避免這個問題 &#8230; </p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// 以上省略
ob_clean();
@readfile($file);</pre>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/10668/%e7%84%a1%e6%b3%95%e9%96%8b%e5%95%9f-php-ziparchive-%e4%b8%8b%e8%bc%89%e7%9a%84-zip-%e6%aa%94/" data-wpel-link="internal">無法開啟 PHP ZipArchive 下載的 zip 檔</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/10668/%e7%84%a1%e6%b3%95%e9%96%8b%e5%95%9f-php-ziparchive-%e4%b8%8b%e8%bc%89%e7%9a%84-zip-%e6%aa%94/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
