我有一个zip函数,我希望FOLDER中的每个文件都压缩
因此,路径是:
public_html/form/FOLDER/file_to_zip.xml
正如您在下面的代码中看到的,$files_to_zip数组具有"FOLDER/file_to_zip.xml"路径,无论何时压缩,也压缩文件夹,我只希望文件被压缩。
我在这里能做什么?
function create_zip($files = array(),$destination = '',$overwrite = true) {
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
$zip->close();
return file_exists($destination);
}
else
{
return false;
}
}
$loja=$_GET['loja'];
$connect = new MySQL('localhost','iloja_phc','sexparade');
$connect->Database('iloja_phc');
$posts = $connect->Fetch('ipad');
if ($posts && mysql_num_rows($posts) > 0) {
echo "Nome - ID - Email:<BR>";
while ($record = mysql_fetch_array($posts)) {
$files_to_zip = array($loja.'/CL'.$record['rand'].'.xml');
$result = create_zip($files_to_zip,'CL'.$record['rand'].'.zip');
echo "<a href='/formulario/CL".$record['rand'].".zip'>".$record['nome']."</a> - ".$record['id']." - EMAIL: ".$record['email']."</br>";
}
} else {
echo "Erro! Algo correu mal...";
}
当您调用时
$zip->addFile($file,$file);
第一个参数是文件的路径,第二个参数是"localname"。如果从第二个参数中删除文件夹,您将获得所需的内容。
示例:
$zip->addFile("foo/bar.txt","bar.txt);
对于您的特殊情况,我认为这应该有效:
$zip->addFile($file, basename($file));
来源:PHP手册评论http://php.net/manual/en/ziparchive.addfile.php