Created
August 17, 2012 09:32
-
-
Save Develle/3377440 to your computer and use it in GitHub Desktop.
Déplacement et suppression de fichiers à partir de données provenant d'une BDD
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 | |
/* | |
* Déplacement et suppression de fichiers à partir de données provenant d'une BDD | |
*/ | |
$dirSource = '/home/www/source/'; | |
$dirDest = '/home/www/dest/'; | |
$mysqli = new mysqli('localhost','root','pwd','bdd'); | |
if( $mysqli->connect_errno ){ | |
printf("Échec de la connexion : %s\n", $mysqli->connect_error); | |
exit(); | |
} | |
if( $res = $mysqli->query("SELECT idPubli,year,bibTex FROM publidocs,publis WHERE publis.id=idPubli;") ){ | |
echo $res->num_rows."\n"; | |
if ($res->num_rows == 0) { | |
echo "Aucune ligne trouvée, rien à afficher."; | |
exit; | |
} | |
while( $row = $res->fetch_assoc() ){ | |
copy_directory( $dirSource . $row['year'] . '/' . $row['bibTex'], $dirDest . $row['idPubli'] ); | |
unlink( $dirDest .$row['idPubli'] . '/index.php' ); | |
del_directory( $dirDest . $row['idPubli'] . '/private/' ); | |
} | |
} | |
function copy_directory( $source, $destination ) | |
{ | |
if( is_dir( $source ) ){ | |
@mkdir( $destination ); | |
$directory = dir( $source ); | |
while( FALSE !== ( $readdirectory = $directory->read() ) ){ | |
if( $readdirectory == '.' || $readdirectory == '..' ){ | |
continue; | |
} | |
$PathDir = $source . '/' . $readdirectory; | |
if( is_dir( $PathDir ) ){ | |
copy_directory( $PathDir, $destination . '/' . $readdirectory ); | |
continue; | |
} | |
copy( $PathDir, $destination . '/' . $readdirectory ); | |
} | |
$directory->close(); | |
}else { | |
copy( $source, $destination ); | |
} | |
} | |
function del_directory( $source ) | |
{ | |
if( is_dir( $source ) ){ | |
if( is_file( $source.'.htaccess' ) ) unlink( $source.'.htaccess' ); | |
$files = scandir( $source ); | |
if( count($files) <= 2 ){ | |
rmdir($source); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment