0

Hello i am using the code bellow to write the following line inside a .php file.

$stringDatar = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML+RDFa 1.0//EN'\n";
fwrite($handler, $stringDatar);

I am trying to do the same to write inside a php file with the following code but it doesn't wite the php code.

$stringDatar = "'.<?php $_GET['p']\n.'";
fwrite($handler, $stringDatar);

Any ideas?

2
  • What must be in file? Can you provide example of output? Commented Aug 3, 2015 at 9:55
  • how are you calling fopen, is $handler a resource, or does the fopen call fail? What mode is the handle in (a, w, ...?) any specific errors/warnings (turn display_errors on, and set the error_reporting to the maximum setting (E_STRICT|E_ALL). You're also doing weird things with the dots (concat operator). To concatenate string, the . shouldn't be inside the quotes: $foo = 'a string'; $foo = '.rest'; reassings $foo, to concatenate: $foo .= 'rest'; (note .=) or $foo = $foo . 'rest'; It's basic syntax Commented Aug 3, 2015 at 9:56

2 Answers 2

1

Variables are expanded inside double quotes, so it's putting the value of $_GET['p'] into the file. You need to escape the dollar sign so it will be written literally.

$stringDatar = "'.<?php \$_GET['p']\n.'";
Sign up to request clarification or add additional context in comments.

Comments

1

you can use this format which might be more more comfortable

<?php

$content = <<< END
<?php
  \$d = 5;
  echo "\$d".PHP_EOL;
  echo \$_GET['arg'];

END;
file_put_contents("filename.php", $content);

fileContents:

<?php
  $d = 5;
  echo "$d".PHP_EOL;
  echo $_GET['arg'];

3 Comments

Personally, I'd also advise against using "\n" in favor of the predefined PHP_EOL constant
ok, but it's just an example, you can also say that $d is not a good variable name, doesn't matter for the example (:
Sure, just added the comment for completeness -> the OP is clearly in the early stages of learning, so I figured it's best to expose them to code that shows "best practices" as much as possible... there's a ton of crap-code out there already (not saying your answer is crap, just saying that many tutorials contain bad code examples)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.