С чего начать ... позвольте мне сосчитать пути.
function update_GTour_theme_files()
{
$new_update = file_get_contents(__DIR__ . "/../../themes/grandtour/header.php");
$new_update = preg_replace('/$page_menu_transparent = 1/','$page_menu_transparent = 0',$new_update);
$new_update = preg_replace('/$grandtour_page_menu_transparent = 1/','$grandtour_page_menu_transparent = 0',$new_update);
file_put_contents (__DIR__ . "/../../themes/grandtour/header.php", $new_update);
if ( file_exists (__DIR__ . "/../../themes/grandtour/header.php") && is_writable (__DIR__ . "/../../themes/grandtour/header.php") ){
update_GTour_theme_files();
echo '</br><span style="color:green;font-weight:bold;">Changes were applied successfully.</span>';
}
else {
echo '</br><span style="color:red;font-weight:bold;">Error occured while applying the changes.</span>';
}
}
Посмотрим, когда вы запустите это:
file_put_contents (__DIR__ . "/../../themes/grandtour/header.php", $new_update);
Затем он проверяет:
if ( file_exists (__DIR__ . "/../../themes/grandtour/header.php") && is_writable (__DIR__ . "/../../themes/grandtour/header.php") ){
update_GTour_theme_files();
echo '</br><span style="color:green;font-weight:bold;">Changes were applied successfully.</span>';
}
Что очевидно верно, иначе мы бы уже получили некоторые ошибки. Так что это в основном всегда верно. Это означает, что вы снова называете это update_GTour_theme_files();
. Повторите вышеуказанные шаги бесконечное количество раз.
Так что это явно неправильно. Если вы звоните по этому номеру, я ожидаю, что ваш браузер заблокируется.
Итак, давайте исправим это (один файл):
function update_GTour_theme_files($file)
{
//fail early
if (!file_exists ($file) || !is_writable ($file) ) die("File $file Does not exists or is not writable");
$new_update = file_get_contents($file);
$new_update = preg_replace('/\$page_menu_transparent\s*=\s*1;/','$page_menu_transparent = 0;',$new_update);
$new_update = preg_replace('/\$grandtour_page_menu_transparent\s*=\s*1;/','$grandtour_page_menu_transparent = 0;',$new_update);
if(file_put_contents ($file, $new_update)){
echo '</br><span style="color:green;font-weight:bold;">Changes were applied successfully.</span>';
}else{
echo '</br><span style="color:red;font-weight:bold;">Error occured while applying the changes.</span>';
}
}
update_GTour_theme_files(__DIR__ . "/../../themes/grandtour/header.php");
Это обновит только один файл, чтобы сделать больше, чем вам нужно использовать scandir
, glob
или SPL DirectoryIterator / FilesystemIterator
.
PS Ваша «главная» или «большая» проблема (кроме рекурсии) прямо здесь:
$new_update = preg_replace('/$page_menu_transparent = 1/','$page_menu_transparent = 0',$new_update);
$new_update = preg_replace('/$grandtour_page_menu_transparent = 1/','$grandtour_page_menu_transparent = 0',$new_update);
Эти $
в /$page_menu_transparent
не экранированы, поэтому они рассматриваются как REGEX. Это означает, что они соответствуют концу строки, что не имеет смысла. Я также добавил немного свободного пространства \s*=\s*
и точка с запятой ;
в противном случае $page_menu_transparent = 1345;
станет $page_menu_transparent = 0;
. Это может иметь некоторое влияние, если оно находится в ()
или массиве и т. Д. (Что-либо без ;
)
Для всех файлов в данной папке и ее подпапках
function update_GTour_theme_files($dir)
{
if (!file_dir($dir) || is_writable ($dir) ) die("Dir $dir Does not exists or is not writable");
$Iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$dir,
RecursiveDirectoryIterator::SKIP_DOTS|RecursiveDirectoryIterator::UNIX_PATHS
)
);
foreach($Iterator as $fileInfo){
if($fileInfo->isDir() || $fileInfo->getExtension() != 'php') continue;
$file = $fileInfo->getPathname();
$new_update = file_get_contents($file);
$new_update = preg_replace('/\$page_menu_transparent\s*=\s*1;/','$page_menu_transparent = 0;',$new_update);
$new_update = preg_replace('/\$grandtour_page_menu_transparent\s*=\s*1;/','$grandtour_page_menu_transparent = 0;',$new_update);
if(file_put_contents ($file, $new_update)){
echo '</br><span style="color:green;font-weight:bold;">Changes were applied successfully.</span>';
}else{
echo '</br><span style="color:red;font-weight:bold;">Error occured while applying the changes.</span>';
}
}
}
update_GTour_theme_files(__DIR__ . "/../../themes/grandtour/");
Используется RecursiveDirectoryIterator
, поэтому он должен просматривать все подпапки.
Но это все непроверенные , так что будьте очень осторожны. Если вы испортите свои файлы не обвиняйте меня, вы были предупреждены.
Это сказал Наслаждайся ~