php 创建 .zip 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11540339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating .zip file
提问by Sam
I am trying to save filesfrom one folderto another. zip folderplaced in different directory. And I have written the following codes:
我正在尝试将文件从一个文件夹保存到另一个文件夹。zip 文件夹放在不同的目录中。我写了以下代码:
archive.php
档案.php
<?php
$zip = new ZipArchive();
$zip->open('example.zip', ZipArchive::CREATE);
$srcDir = "/home/sam/uploads/";
$files= scandir($srcDir);
//var_dump($files);
unset($files[0],$files[1]);
foreach ($files as $file) {
$zip->addFile("{$file}");
}
$zip->close();
?>
But sadly I am not able to create the .zip folder. Is there any step I missed?
但遗憾的是我无法创建 .zip 文件夹。有没有我遗漏的步骤?
采纳答案by Dhruvisha
$zip = new ZipArchive();
$DelFilePath="first.zip";
if(file_exists($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath)) {
unlink ($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath);
}
if ($zip->open($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath, ZIPARCHIVE::CREATE) != TRUE) {
die ("Could not open archive");
}
$zip->addFile("file_path","file_name");
// close and save archive
$zip->close();
Here TEST is your project folder name.
这里 TEST 是您的项目文件夹名称。
You can define path as you want.
您可以根据需要定义路径。
回答by Sam
Yes I have solved my problem .
是的,我已经解决了我的问题。
Here I have just replaced the code
这里我刚刚替换了代码
$zip->addFile("{$file}");
with the code
用代码
$zip->addFromString(basename($file), file_get_contents($file));
and get my work done. :)
并完成我的工作。:)
回答by Indrajeet Singh
Try this code:
试试这个代码:
<?php
$zip = new ZipArchive;
if ($zip->open(getcwd() . '/test.zip', ZipArchive::CREATE) === TRUE) {
$zip->addFile(getcwd() . '/file.txt', 'newname.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
回答by phphunger
Please see this below example:
请看下面的例子:
<?php
$error = ""; //error holder
if(isset($_POST['createpdf'])){
$post = $_POST;
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip')){ // Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0){ // Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach($post['files'] as $file){
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name)){
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}else
$error .= "* Please select file to zip <br/>";
}else
$error .= "* You dont have ZIP extension<br/>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download As Zip</title>
</head>
<body>
<center><h1>Create Zip</h1></center>
<form name="zips" method="post">
<?php if(!empty($error)) { ?>
<p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; width:588px; margin:0 auto 10px;"><?php echo $error; ?></p>
<?php } ?>
<table width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid;">
<tr>
<td width="33" align="center">*</td>
<td width="117" align="center">File Type</td>
<td width="382">File Name</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="a.jpg" /></td>
<td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
<td>a.jpg</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="b.jpg" /></td>
<td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
<td>b.jpg</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="c.docx" /></td>
<td align="center"><img src="files/doc.png" title="Document" width="16" height="16" /></td>
<td>c.docx</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="d.pdf" /></td>
<td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td>
<td>d.pdf</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" name="createpdf" style="border:0px; background-color:#800040; color:#FFF; padding:10px; cursor:pointer; font-weight:bold; border-radius:5px;" value="Download as ZIP" />
<input type="reset" name="reset" style="border:0px; background-color:#D3D3D3; color:#000; font-weight:bold; padding:10px; cursor:pointer; border-radius:5px;" value="Reset" />
</td>
</tr>
</table>
</form>
</body>
</html>

