Reputation: 327
I have one zip function, and i want each file inside FOLDER to zip
So, the path is:
public_html/form/FOLDER/file_to_zip.xml
As you can see in the code bellow, the $files_to_zip array has the "FOLDER/file_to_zip.xml" path and whenever it zips, also zips the folder, i only want the file to get zipped.
What can i do here?
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...";
}
Upvotes: 2
Views: 2071
Reputation: 6175
When you're calling
$zip->addFile($file,$file);
the first parameter is the path to the file, and the second is the "localname". If you remove the folder from the second parameter you'll get what you need.
Example:
$zip->addFile("foo/bar.txt","bar.txt);
For your particular case, I think this should work:
$zip->addFile($file, basename($file));
Source: PHP manual comments http://php.net/manual/en/ziparchive.addfile.php
Upvotes: 4