PHPZip\Zip\File\Zip::sendZip PHP Method

sendZip() public method

Send the archive as a zip download
Author: A. Grandt ([email protected])
public sendZip ( String $fileName = null, String $contentType = self::CONTENT_TYPE, String $utf8FileName = null, boolean $inline = false ) : boolean
$fileName String The name of the Zip archive, in ISO-8859-1 (or ASCII) encoding, ie. "archive.zip". Optional, defaults to null, which means that no ISO-8859-1 encoded file name will be specified.
$contentType String Content mime type. Optional, defaults to "application/zip".
$utf8FileName String The name of the Zip archive, in UTF-8 encoding. Optional, defaults to null, which means that no UTF-8 encoded file name will be specified.
$inline boolean Use Content-Disposition with "inline" instead of "attachment". Optional, defaults to false.
return boolean $success
    public function sendZip($fileName = null, $contentType = self::CONTENT_TYPE, $utf8FileName = null, $inline = false) {
        if (!$this->isFinalized) {
            $this->finalize();
        }

        if ($this->buildResponseHeader($fileName, $contentType, $utf8FileName, $inline)) {
            return true;
        }
        return false;
    }

Usage Example

Example #1
0
	private static function _createAndSendZipArchive(ZipArchiveFile $zip, $fileDir = './'){

		// Archive comments don't really support utf-8. Some tools detect and read it though.
		$zip->setComment("Example Zip file.\nАрхив Комментарий\nCreated on " . date('l jS \of F Y h:i:s A'));

		// A bit of russian (I hope), to test UTF-8 file names.
		$zip->addFile("Привет мир!", "Кириллица имя файла.txt");
		$zip->addFile("Привет мир!", "Привет мир. С комментарий к файлу.txt", 0, "Кириллица файл комментарий");
		$zip->addFile("Hello World!", "hello.txt");

		@$handle = opendir($fileDir);
		if ($handle) {
			while (false !== ($file = readdir($handle))) {
				if (strpos($file, ".php") !== false) {
					$pathData = pathinfo($fileDir . $file);
					$fileName = $pathData['filename'];

					$zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file), null, true, ZipUtils::getFileExtAttr($file));
				}
			}
		}

		// Add a directory, first recursively, then the same directory, but without recursion.
		// Naturally this requires you to change the path to ../test to point to a directory of your own.
		// $zip->addDirectoryContent("testData/test", "recursiveDir/test");
		// $zip->addDirectoryContent("testData/test", "recursiveDir/testFlat", FALSE);

		//$zip->sendZip("ZipExample1.zip");
		//$zip->sendZip("ZipExample1_€2,000.zip");
		$zip->sendZip("ZipExample1_€2,000.zip", "application/zip", "ZipExample1_€2,000_utf8.zip");

	}