Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maverabilisim/63f854fe5ac0254ee64f951886d611fc to your computer and use it in GitHub Desktop.
Save maverabilisim/63f854fe5ac0254ee64f951886d611fc to your computer and use it in GitHub Desktop.
gives file list to zip arcive with fancy path
<?php
/*START FILES TO ZIP*/
ini_set('max_execution_time', 600);
ini_set('memory_limit', '1024M');
// Start the backup!
function zipData($files, $destination) {
$isSucess = false;
if (extension_loaded('zip')) {
if(file_exists($destination)){
unlink($destination);
}
$zip = new ZipArchive();
if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
if ($files) {
foreach ($files as $file) {
$file = realpath($file);
$fileName = basename($file);
//pre(base);
//pre($file);continue;
if (is_dir($file)) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
} else if (is_file($file)) {
$filePath = str_replace($fileName,"",$file);
pre($filePath,"",'$filePath: ');
$vhostFile = str_replace($_SERVER['DOCUMENT_ROOT']."/","",$file);
pre($vhostFile,"",'$vhostFile: ');
$vhostPath = str_replace($fileName,"",$vhostFile);
pre($vhostPath,"",'$vhostPath: ');
$zip->addEmptyDir($vhostPath);
$zip->addFromString($vhostFile, file_get_contents($file));
}
}
}
}
$isSucess = $zip->close();
}
if($isSucess){
echo '<a href="'.$destination.'">'.$destination.'</a>';
}else{
echo "Dosya oluşturulamadı";
}
}
$sql = "SELECT dosya_adi FROM medyalar WHERE modul='personel'";
$files = $db->get_results($sql,ARRAY_A);
$fileList = [];
if($files){
foreach($files as $file){
$fileList[] = $file['dosya_adi'];
}
}
unset($files);
if(count($fileList)>0){
zipData($fileList, './backup.zip');
}
/*END FILES TO ZIP*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment