how to do backup to website in php code

Solutions on MaxInterview for how to do backup to website in php code by the best coders in the world

showing results for - "how to do backup to website in php code"
Greta
28 Jan 2019
1if (!file_exists('/home/sites/'.$_SERVER['SERVER_NAME'].'/cbitsbackup/')) {
2mkdir('/home/sites/'.$_SERVER['SERVER_NAME'].'/cbitsbackup/', 0777, true);
3}
4$filefront = '/home/sites/'.$_SERVER['SERVER_NAME'].'/cbitsbackup/backup-'.$_SERVER['SERVER_NAME'].'-day1';
5$fileend = '.zip';
6$time = time();
7
8if(file_exists($filefront.$fileend)){
9$file = $filefront.'-'.$time.$fileend;}
10else{$file = $filefront.$fileend;};
11zip_directory('/home/sites/'.$_SERVER['SERVER_NAME'].'/public_html',$file);
12
13
14function zip_directory($source,$tempfile){
15if(!extension_loaded('zip') || !file_exists($source)) return false;
16$zip = new ZipArchive();
17if(!$zip->open($tempfile,ZIPARCHIVE::CREATE)) return false;
18$source = str_replace('\\','/',realpath($source));
19if(is_dir($source) === true){
20    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
21    foreach($files as $file){
22        $file = str_replace('\\', '/', realpath($file));
23        if(is_dir($file) === true) $zip->addEmptyDir(str_replace($source . '/','', $file . '/'));
24        else if(is_file($file) === true) $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));}}
25elseif(is_file($source) === true) $zip->addFromString(basename($source), file_get_contents($source));
26return $zip->close();}
27