如何创建rar/zip文件和下载
问题描述:
嗨frnds任何人都可以打电话给我如何使用PHP创建一个Zip文件? 其实我有10个文件的框,如果我选择多个复选框,文件应该得到zip/rar,我可以保存在一些路径..如何创建rar/zip文件和下载
请任何人都可以在此解决方案的帮助,因为我我新的PHP
答
$zip_name = 'path/to/final.zip'; //the real path of your final zip file on your system
$zip = new ZipArchive;
$zip->open($zip_name, ZIPARCHIVE::CREATE);
foreach($files as $file)
{
$zip->addFile($file);
}
$zip->close();
header('Content-type: application/zip');
header('Content-disposition: filename="' . $zip_name . '"');
header("Content-length: " . filesize($zip_name));
readfile($zip_name);
exit();
答
// This example creates a ZIP file archive test.zip and add the file /path/to/index.txt. as newname.txt.
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFile('/path/to/index.txt', 'newname.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
+0
wht is that test.zip 也dint get('/path/to/index.txt','newname.txt'); 实际上我想为word文档创建zip – user346797 2010-07-02 09:37:04
答
//include your connection file
//$file_names[] get your files array
//$error = ""; //error holder
//$file_path='set your image path' folder to load files
if(extension_loaded('zip'))
{ // Checking ZIP extension is available
if(count($file_names) > 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($file_names as $file){
$zip->addFile($file_path.$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/>";
}
?>
路径/到/ final.zip这是这条道路?我想在我的系统中安装winzip?实际上我正在使用winrar .. – user346797 2010-07-02 09:08:38
编辑了我关于路径的答案。不,你不需要安装任何第三方软件。 ZipArchive是PHP内置的Zip扩展。你可以在PHP 5.2.0+ – fabrik 2010-07-02 09:13:47
开箱即用的情况下使用它,但是无论我在复选框中选择哪些文件,文件不会进入zipfolder..wat是问题所在? – user346797 2010-07-02 09:27:23