Last active
August 29, 2015 14:20
-
-
Save Xiradorn/6b52074455c3aecffbbe to your computer and use it in GitHub Desktop.
Copia e lettura cartella file ricorsiva in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Funzioni per file e directory | |
* Version: 4.15.04.28-01:02 | |
* Author: Sir Xiradorn | |
* xiradorn.it - 2015 | |
*/ | |
/**** // Funzione di uso // ****/ | |
/// Funzione di copia globalizzata | |
function copia($source, $dest) { | |
crea_dir($dest); | |
//copia ricorsiva | |
copia_ricorsiva($source, $dest); | |
} | |
//funzione di copia ricorsiva ad albero | |
function copia_ricorsiva($source, $dest) { | |
//controllo di esistenza della directory | |
/*if(!file_exists($dest)){mkdir($dest);echo "Cartella $dest non esistente. Creazione avvenuta con successo <br>";}else{echo "Cartella $dest gia presente. Procedo con la copia <br>";}*/ | |
if (!file_exists($dest)) { | |
mkdir($dest); | |
echo "Cartella $dest non esistente. Creazione avvenuta con successo <br>"; | |
} else { | |
echo "Cartella $dest gia presente. Procedo con la copia <br>"; | |
} | |
foreach(scandir($source) as $file) { | |
if(!is_readable($file_source=($source.'/'.$file))) continue; | |
if(is_dir($file_source=($source.'/'.$file)) && ($file != '..') && ($file != '.')) { | |
$file_dest = preg_replace("#\s#", "", $dest.'/'.$file); | |
//mkdir(str_replace(" ", "_", $file_dest)); | |
copia_ricorsiva($file_source, $file_dest); | |
echo "/$file_source/ -> /$file_dest/<br>"; | |
} elseif (($file != '..') && ($file != '.')) { | |
$file_source=($source.'/'.$file); | |
$file_dest=($dest.'/'.$file); | |
copy($file_source, $file_dest); | |
echo "$s/$file_source -> $s/$file_dest "; | |
$hash_source[] = hash_check($file_source); | |
$hash_dest[] = hash_check($file_dest); | |
$hash_algo = array("md5", "sha1"); | |
foreach ($hash_algo as $h) { | |
if($hash_source[$h] == $hash_dest[$h]) { | |
echo "<span style='font-weight:bold; color: green;'>$h OK</span> - "; | |
} else { | |
echo "<span style='font-weight:bold; color: red;'>$h errato</span> -"; | |
} | |
} | |
} | |
} | |
} | |
// Funzione di creazione direcory o directories mancanti | |
function crea_dir($path) { | |
//controllo di esistenza della directory | |
if (!file_exists($path)) { | |
$dir = explode("/", $path); | |
foreach ($dir as $d) { | |
$a .= $d . "/"; | |
echo $a . "<br>"; | |
mkdir($a); | |
echo "Cartella $a non esistente. Creazione avvenuta con successo <br>"; | |
} | |
} else { | |
echo "Cartella $path gia presente. Procedo con la copia <br>"; | |
} | |
} | |
// Codifica in Hash | |
function hash_check($file, $rapid=true) { | |
if(!$rapid) { | |
foreach (hash_algos() as $h) { | |
$hash[$h] = hash($h, file_get_contents($file)); | |
} | |
} else { | |
$hash_algo_list_rapid = array("md5", "sha1", "crc32"); | |
foreach ($hash_algo_list_rapid as $h) { | |
$hash[$h] = hash($h, file_get_contents($file)); | |
} | |
} | |
return $hash; | |
} | |
// Funzione di lettura ricorsiva ad albero | |
function albero_file_dir($s) { | |
if (is_dir($s)) { | |
$handle = opendir($s); | |
while ( ($file = readdir($handle)) !== false ) { | |
if(is_dir($files=$s.'/'.$file) && ($file != '..') && ($file != '.')) { | |
leggidir($files); | |
echo "$files/ <br>"; | |
} else if(($file != '..') && ($file != '.')) { | |
echo "$s/$file <br>"; | |
} | |
} | |
closedir($handle); | |
} | |
} | |
copia("filebase", "test/test/provolone/provolozza/gatto"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment