<?php
define('THIS_FILE',
__FILE__
);
define('THIS_PATH', dirname(THIS_FILE));
define('Cover', '
new
');
define('DS', DIRECTORY_SEPARATOR);
define('ICONV', 'UTF-8');
function
eachFile(
$path
,
$files
= [])
{
if
(Cover !== true &&
$path
== THIS_PATH . DS . Cover) {
return
$files
;
}
if
(preg_match(
"/[\x7f-\xff]/"
,
$path
)) {
$path
= iconv('UTF-8', 'GBK',
$path
);
}
if
(
is_file
(
$path
)) {
$files
[] =
$path
;
return
$files
;
}
$list
= scandir(
$path
);
foreach
(
$list
as
$k
=>
$v
) {
if
(
$v
!== '.' &&
$v
!== '..') {
$p
=
$path
. DS .
$v
;
if
(preg_match(
"/[\x7f-\xff]/"
,
$p
)) {
$p
= iconv('UTF-8', 'GBK',
$p
);
}
if
(
is_dir
(
$p
)) {
$files
= eachFile(
$p
,
$files
);
}
else
{
$files
[] =
$p
;
}
}
}
return
$files
;
}
$files
= eachFile(THIS_PATH);
foreach
(
$files
as
$k
=>
$v
) {
$ext
=
pathinfo
(
$v
, PATHINFO_EXTENSION);
if
(in_array(
$ext
, ['txt', 'php', 'css', 'js', 'htm', 'html'])) {
if
(
$v
== THIS_FILE)
continue
;
$contents_before
=
file_get_contents
(
$v
);
$oldIconv
= mb_detect_encoding(
$contents_before
,
array
('ASCII', 'GB2312', 'GBK', 'UTF-8', 'BIG5'));
$contents_after
= iconv(
$oldIconv
, ICONV,
$contents_before
);
if
(Cover !== true) {
$newPath
=
str_replace
(THIS_PATH, THIS_PATH . DS . Cover,
$v
);
if
(!
file_exists
(dirname(
$newPath
))) {
mkdir
(dirname(
$newPath
), 0755, true);
}
file_put_contents
(
$newPath
,
$contents_after
);
}
else
{
file_put_contents
(
$v
,
$contents_after
);
}
echo
"{$v} 已转换<hr>"
;
}
else
{
$newPath
=
str_replace
(THIS_PATH, THIS_PATH . DS . Cover,
$v
);
if
(Cover !== true && !
file_exists
(
$newPath
)) {
if
(!
file_exists
(dirname(
$newPath
))) {
mkdir
(dirname(
$newPath
), 0755, true);
}
copy
(
$v
,
$newPath
);
echo
"{$v} 复制文件到新路径 {$newPath}<hr>"
;
}
}
}