function backup_create($src, $dst, $types = array()) { # get the relative path to create a backup with same structure $src_path = $src; $dst_path = $dst; if (preg_match('#[a-z]{3,4}[\\d]{0,1}$#', $src) && preg_match('#[a-z]{3,4}[\\d]{0,1}$#', $dst)) { // both are a file $dst_path = explode('/', $dst); array_pop($dst_path); $dst_path = implode('/', $dst_path); } else { if (!preg_match('#/$#', $src) && !preg_match('#/$#', $dst)) { return; } } if (!CPG_File::analyze_path($dst_path)) { return; } if (is_dir($src) && ($list = scandir($src))) { $i = 0; $content = array(); while ($file = array_shift($list)) { $content[$i]['source'] = $src_path . $file; $content[$i]['destination'] = $dst_path . $file; ++$i; } } else { $content = array(0 => array('source' => $src, 'destination' => $dst)); } foreach ($content as $file) { // selective and no recursive backups for the moment if (is_dir($file['source']) || !preg_match('#\\.(php[\\d]?|inc)$#', $file['source']) || file_exists($file['destination'])) { continue; } else { if (copy($file['source'], $file['destination'])) { continue; } else { if (CPG_File::copy_special($file['source'], $file['destination'])) { continue; } } } // runs analyze_path again trigger_error('Couldn\'t copy or write the destination file', E_USER_WARNING); return; } return true; }
public function copy_special($oldfile, $newfile) { if (!CPG_File::analyze_path(dirname($newfile))) { return false; } if (!($of = fopen($oldfile, 'rb'))) { return false; } if (!($nf = fopen($newfile, 'wb'))) { fclose($of); return false; } while (!feof($of)) { if (fwrite($fp, fread($of, 2048)) === FALSE) { fclose($of); fclose($fp); return false; } } fclose($of); fclose($fp); chmod($newfile, PHP_AS_NOBODY ? 0666 : 0644); return true; }