Place content inside PHP fopen

Asked

Viewed 47 times

-2

Good afternoon, I am creating a new page to run a command by fopen, with the following code:

<?php
  $pagina  = 'pagina';
  $ext     = '.php';
  $arquivo = fopen ($pagina.$ext, 'w+');
  if ($arquivo == false) die('não foi possível criar o arquivo');
?>

the doubt is, how do I put a predetermined content inside the new file that was created, for example an echo, or so that the new file is created inside it already came an echo?

1 answer

1


For this, you must use the function fwrite.

<?php

  $pagina  = 'pagina';
  $ext     = '.php';
  $arquivo = fopen ($pagina.$ext, 'w+');
  fwrite($arquivo, '<?php echo "Qualquer texto inicial."; ?>');

  if ($arquivo == false) die('não foi possível criar o arquivo');

?>
  • Cool Jose! but I could put an html code? or not?

  • 1

    Yes, I could, just change the contents. No secret! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.