@Jony1337

PHP создаёт php как это сделать?

Привет всем
сегодня попробовал создать файл php при помощи php
Допустим есть такой php код
$nD = "index";
$nS = "example";
$html = <<<HERE
<?php
$y = rand(1, 9) ; 
?>
<html>
<head>
<title>Master</title>
<script>
setTimeout( 'location="http://site.ru/maps/$nD.php?id=<? echo $y;?>";', 100 );
</script>

</head>
<body>
</body>
</html>
HERE;
$var = "$html";
	$f = fopen("maps/$nS.php","w+");
	fwrite($f,$var); 
	fclose($f);

и вот что за файл создаётся

<?php
 = rand(1, 9) ; 
?>

<html>
<head>
<title>Master</title>
<script>
setTimeout( 'location="http://site.ru/maps/example.php?y=<? echo ;?>";', 100 );
</script>

</head>
<body>
</body>
</html>
  • Вопрос задан
  • 367 просмотров
Решения вопроса 3
@kryvel
Системный администратор
Возможно в Вашем случае лучше будет использовать NOWDOC вместо HEREDOC,
вот здесь можно почитать об этом
php.net/manual/en/language.types.string.php#langua...
php.net/manual/en/language.types.string.php#langua...

ну или экранировать знак $ для правильного парсинга
Ответ написан
Комментировать
webinar
@webinar Куратор тега PHP
Учим yii: https://youtu.be/-WRMlGHLgRg
Попробуйте: php.net/manual/ru/function.file-put-contents.php
$file = 'путь/до/файла'
$string = 'Ваш код';
file_put_contents($file, $string);
Ответ написан
Комментировать
слеш добавь перед спец символами

$nD = "index";
$nS = "example";
$html = "
<?php
\$y = rand(1, 9) ; 
?>
<html>
<head>
<title>Master</title>
<script>
setTimeout( 'location= \"http://site.ru/maps/$nD.php?id=<? echo \$y;?>\";', 100 );
</script>

</head>
<body>
</body>
</html>";

$var = "$html";
  $f = fopen("maps/$nS.php","w+");
  fwrite($f,$var); 
  fclose($f);


результат:

<?php
$y = rand(1, 9) ; 
?>
<html>
<head>
<title>Master</title>
<script>
setTimeout( 'location= "http://site.ru/maps/index.php?id=<? echo $y;?>";', 100 );
</script>

</head>
<body>
</body>
</html>
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы