1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| <?php
header('Content-type:text/html; charset=UTF-8'); // encodage UTF-8
error_reporting(E_ALL); // en TEST !!
// -----------------------------------------------
// Vérification/Conversion d'un fichier en UTF-8
function toUTF8($file) {
if(!file_exists($file)) return false; // le fichier n'existe pas
// IMPORTANT : liste des extensions autorisées pour les fichiers à tester/encoder
$file_Extension_OK = array('php','htm','html','xml','txt','css');
$file_Extension = strtolower(pathinfo($file,PATHINFO_EXTENSION));
if(!in_array($file_Extension, $file_Extension_OK)) return false; // extension pas ok
$contents = file_get_contents($file);
$contentsbefore = $contents;
$contents = html_entity_decode($contents);
if(mb_check_encoding($contents, 'UTF-8')){
if($contentsbefore !== $contents){
file_put_contents($file, $contents);
// return true;
return 'html_entity_decode : '.$file;
} else {
// return false; // c'est déjà UTF-8
return 'ok : '.$file;
}
}
file_put_contents($file, utf8_encode($contents));
// return true;
return 'utf8_encode : '.$file;
};
// -----------------------------------------------
// Explorateur de dossier (récursif)
//function explore_dir_scan_html($dir, $niv=0, $id=0)
function dir_convert_toUTF8($dir, $niv=0, $id=0) // fonction modifiée
{
$html = null;
$html_dirs = null;
$html_fils = null;
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
$id++;
if(is_dir($dir."/".$entry)) // dossier
{
if($entry!='..' && $entry!='.')
{
$html_dirs .= str_repeat("\t",$niv+1)."<li class='dir' id='div_".$id."'>".$entry."\n";
$html_dirs .= str_repeat("\t",$niv+2)."<ul class='sub_dir' id='sub_".$id."'>\n";
$html_dirs .= dir_convert_toUTF8($dir."/".$entry, $niv+1, $id);
}
} else { // fichier
// ------------
// partie modifiée
// $html_fils .= str_repeat("\t",$niv+2)."<li class='fil' id='fil_".$id."'><a href='".$dir."/".$entry."' target='_blank'>".$entry."</a></li>\n";
$toUTF8 = toUTF8($dir.'/'.$entry);
if(!empty($toUTF8)){
$html_fils .= str_repeat("\t",$niv+2)."<li class='fil' id='fil_".$id."'>".$toUTF8."</li>\n";
}
// ------------
}
if(is_dir($dir."/".$entry))
{
if($entry!='..' && $entry!='.')
{
$html_dirs .= str_repeat("\t",$niv+2)."</ul>\n";
$html_dirs .= str_repeat("\t",$niv+1)."</li>\n";
}
}
}
closedir($handle);
$html .= $html_dirs; // dossiers
$html .= $html_fils; // fichiers
return $html;
}
};
// -----------------------------------------------
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Dir Explorer - File Converter to UTF-8</title>
<!-- Script initialisation jquery -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#exploreur-dossier').on('click', '.dir', function(event){
$(this).find('ul').first().toggle();
event.stopPropagation(); /* important */
});
});
</script>
<style>
#exploreur-dossier ul { margin:0; padding:0; list-style:none outside none; }
#exploreur-dossier .dir { position:relative; padding-left:20px; border:solid 0px #00FF00; }
#exploreur-dossier .sub_dir { position:relative; padding-left:20px; display:none; border:solid 0px #FF0000; }
#exploreur-dossier .fil { position:relative; padding-left:20px; }
#exploreur-dossier .dir:before { cursor:pointer; position:absolute; content:''; display:block; width:16px; height:16px; top:2px; left:0; background:url(images/folder.png) no-repeat; }
#exploreur-dossier .fil:before { position:absolute; content:''; display:block; width:16px; height:16px; top:2px; left:0; background:url(images/file.png) no-repeat; }
</style>
</head>
<body>
<?php $dossier_a_explorer = "./dossier_a_explorer"; ?>
<h3>Exploration de dossier / Conversion des fichiers en UTF-8</h3>
<h4>Dossier : "<?php echo $dossier_a_explorer; ?>"</h4>
<nav id="exploreur-dossier">
<ul>
<?php echo dir_convert_toUTF8($dossier_a_explorer); // N.B. la fonction étant récursive, on ne peut pas mettre les balises <ul> dedans ?>
</ul>
</nav>
</body>
</html> |
Partager