/** * * @param String $source * @param String $dest * @param Array $options * @return Boolean * @author Sina Salek * @link http://sina.salek.ws/en/contact */ static function copy($source, $dest, $options = array('folderPermission' => 0755, 'filePermission' => 0755)) { $result = false; if (is_file($source)) { if ($dest[strlen($dest) - 1] == '/') { if (!file_exists($dest)) { cmfcDirectory::makeAll($dest, $options['folderPermission'], true); } $__dest = $dest . "/" . basename($source); } else { $__dest = $dest; } $result = copy($source, $__dest); chmod($__dest, $options['filePermission']); } elseif (is_dir($source)) { if ($dest[strlen($dest) - 1] == '/') { if ($source[strlen($source) - 1] == '/') { //Copy only contents } else { //Change parent itself and its contents $dest = $dest . basename($source); @mkdir($dest); chmod($dest, $options['filePermission']); } } else { if ($source[strlen($source) - 1] == '/') { //Copy parent directory with new name and all its content @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } else { //Copy parent directory with new name and all its content @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } } $dirHandle = opendir($source); while ($file = readdir($dirHandle)) { if ($file != "." && $file != "..") { if (!is_dir($source . "/" . $file)) { $__dest = $dest . "/" . $file; } else { $__dest = $dest . "/" . $file; } //echo "$source/$file ||| $__dest<br />"; $result = smartCopy($source . "/" . $file, $__dest, $options); } } closedir($dirHandle); } else { $result = false; } return $result; }
/** * Copy a file or complete directory to a given location * * source=file & dest=dir => copy file from source-dir to dest-dir * source=file & dest=file / not there yet => copy file from source-dir to dest and overwrite a file there, if present * source=dir & dest=dir => copy all content from source to dir * source=dir & dest not there yet => copy all content from source to a, yet to be created, dest-dir * * @param String $source * @param String $dest * @param int $folderPermission [optional] * @param int $filePermission [optional] * @return */ function smartCopy($source, $dest, $folderPermission = 0755, $filePermission = 0644) { $result = false; if (is_file($source)) { // $source is file if (is_dir($dest)) { // $dest is folder if ($dest[strlen($dest) - 1] != '/') { // add '/' if necessary $__dest = $dest . "/"; } $__dest .= basename($source); } else { // $dest is (new) filename $__dest = $dest; } $result = copy($source, $__dest); chmod($__dest, $filePermission); } elseif (is_dir($source)) { // $source is dir if (!is_dir($dest)) { // dest-dir not there yet, create it @mkdir($dest, $folderPermission); chmod($dest, $folderPermission); } if ($source[strlen($source) - 1] != '/') { // add '/' if necessary $source = $source . "/"; } if ($dest[strlen($dest) - 1] != '/') { // add '/' if necessary $dest = $dest . "/"; } // find all elements in $source $result = true; // in case this dir is empty it would otherwise return false $dirHandle = opendir($source); while ($file = readdir($dirHandle)) { // note that $file can also be a folder if ($file != "." && $file != "..") { // filter starting elements and pass the rest to this function again // echo "$source$file ||| $dest$file<br />\n"; $result = smartCopy($source . $file, $dest . $file, $folderPermission, $filePermission); } } closedir($dirHandle); } else { $result = false; } return $result; }
function smartCopy($source, $dest, $folderPermission = 0755, $filePermission = 0644) { # source=file & dest=dir => copy file from source-dir to dest-dir # source=file & dest=file / not there yet => copy file from source-dir to dest and overwrite a file there, if present # source=dir & dest=dir => copy all content from source to dir # source=dir & dest not there yet => copy all content from source to a, yet to be created, dest-dir $result = false; if (is_file($source)) { # $source is file if (is_dir($dest)) { # $dest is folder if ($dest[strlen($dest) - 1] != '/') { # add '/' if necessary $__dest = $dest . "/"; } $__dest .= basename($source); } else { # $dest is (new) filename $__dest = $dest; } $result = copy($source, $__dest); chmod($__dest, $filePermission); } elseif (is_dir($source)) { # $source is dir if (!is_dir($dest)) { # dest-dir not there yet, create it @mkdir($dest, $folderPermission); chmod($dest, $folderPermission); } if ($source[strlen($source) - 1] != '/') { # add '/' if necessary $source = $source . "/"; } if ($dest[strlen($dest) - 1] != '/') { # add '/' if necessary $dest = $dest . "/"; } # find all elements in $source $result = true; # in case this dir is empty it would otherwise return false $dirHandle = opendir($source); while ($file = readdir($dirHandle)) { # note that $file can also be a folder if ($file != "." && $file != "..") { # filter starting elements and pass the rest to this function again # echo "$source$file ||| $dest$file<br />\n"; $result = smartCopy($source . $file, $dest . $file, $folderPermission, $filePermission); } } closedir($dirHandle); } else { $result = false; } return $result; }
$source = DOCUMENTROOT . $file; $dest = $folder; if (is_dir($source) && $source[strlen($source) - 1] != '/') { # add '/' if necessary $source = $source . "/"; } if ($dest[strlen($dest) - 1] != '/') { # add '/' if necessary $dest = $dest . "/"; } if (is_dir($source)) { $dest = $dest . basename(rtrim($source, '/')); } else { $dest = $dest . basename($source); } if (!smartCopy($source, $dest)) { $result = false; } } else { echo 'error||' . translate("The folder path was tampered with!"); break; } } if ($result) { echo 'success||' . translate("The files where successfully copied!"); } else { echo 'error||' . translate("The file or folder path was tampered with!"); } } elseif (!count($files)) { // Array is empty @return true echo 'success||' . sprintf(translate("%d file(s) successfully removed!"), count($files));
public function smartCopy($source, $dest, $options = array('folderPermission' => 0755, 'filePermission' => 0755)) { $result = false; if (is_file($source)) { if ($dest[strlen($dest) - 1] == '/') { if (!file_exists($dest)) { cmfcDirectory::makeAll($dest, $options['folderPermission'], true); } $__dest = $dest . "/" . basename($source); } else { $__dest = $dest; } $result = copy($source, $__dest); chmod($__dest, $options['filePermission']); } elseif (is_dir($source)) { if ($dest[strlen($dest) - 1] == '/') { if ($source[strlen($source) - 1] == '/') { //Copy only contents } else { $dest = $dest . basename($source); @mkdir($dest); chmod($dest, $options['filePermission']); } } else { if ($source[strlen($source) - 1] == '/') { @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } else { @mkdir($dest, $options['folderPermission']); chmod($dest, $options['filePermission']); } } $dirHandle = opendir($source); while ($file = readdir($dirHandle)) { if ($file != "." && $file != "..") { if (!is_dir($source . "/" . $file)) { $__dest = $dest . "/" . $file; } else { $__dest = $dest . "/" . $file; } $result = smartCopy($source . "/" . $file, $__dest, $options); } } closedir($dirHandle); } else { $result = false; } return $result; }
if ($_POST['hidden'] == 'insert') { $date = $db->quote(date("Y-m-d")); $numero = $db->quote($_POST['inputNumero']); $nom = $db->quote($_POST['inputNom']); $client = $db->quote($_POST['inputClient']); $chef_projet = $db->quote($_POST['salarie']); $createur = $db->quote($_SESSION['Auth']['identifiant']); /* print_r("INSERT INTO affaire SET date=$date, numero=$numero, nom=$nom, client=$client, chef_projet=$chef_projet, createur=$createur"); die(); */ umask(00); mkdir(SERVER . $_POST['inputClient'], 0777); mkdir(SERVER . $_POST['inputClient'] . '/' . $_POST['inputNumero'] . '_' . $_POST['inputNom'], 0777); $dest = SERVER . $_POST['inputClient'] . '/' . $_POST['inputNumero'] . '_' . $_POST['inputNom']; smartCopy(SERVER_SRC, $dest); $db->query("INSERT INTO affaire SET date={$date}, numero={$numero}, nom={$nom}, client={$client}, chef_projet={$chef_projet}, createur={$createur}"); setFlash('Affaire créée'); header('Location:' . WEBROOT . 'modules/documents/creation_affaire.php'); die; } } /** * SUPPRESSION AFFAIRE **/ if (isset($_GET['delete'])) { $id = $db->quote($_GET['delete']); $select = $db->query("SELECT * FROM affaire WHERE id={$id}"); $folders = $select->fetchAll(); foreach ($folders as $folder) { rmRecursive(SERVER . $folder['client'] . '/' . $folder['numero'] . '_' . $folder['nom']);
private function _BackupDirectory($pLocation) { if (!mkdir($pLocation)) { return false; } chmod($pLocation, 0777); $source = ASD_PATH; $dest = $pLocation; $backupDirectories = $this->_GetDirectoriesToBackup($source); foreach ($backupDirectories as $d => $directory) { $source = ASD_PATH . DS . $directory; $dest = $pLocation . DS . $directory; smartCopy($source, $dest, array('folderPermission' => 0777, 'filePermission' => 0777)); } return true; }
} /** * COPIE DES FICHIERS SUR LA CARTE USB */ if (isset($_GET['copy'])) { $nom = strtoupper($_SESSION['Auth']['nom']); $dest = SRC_USB . $nom . '/MATRICE'; if (file_exists($dest)) { rmRecursive($dest); } umask(00); mkdir($dest, 0777, true); var_dump("dossier créé"); $src = SRC_MATRICE; var_dump($src); smartCopy($src, $dest); var_dump("recursive"); setFlash('Carte USB mise à jour'); header('Location:' . WEBROOT . 'modules/documents/matrice_doc.php'); die; } include '../../partials/header_index.php'; ?> <div id="wrapper"> <?php include '../../partials/navigation.php'; ?> <div id="page-wrapper">