Doubt about PHP command to write inside another .php file

2

I'm encountering a problem in the following code:

<?php

$id = $_POST['id'];
$titulo = $_POST['titulo'];
$imagem = $_POST['imagem'];
$coment = $_POST['coment'];

$finalcoment = '<div id="triplox"><a name="index-' . $id . '"></a><br><div id="idnoticias"><center><a href="HTML/Noticias/index-' . $id . '.php"><u>' . $titulo . '</u></a></center></div><br><br><center><img width="90%" height="90%" src="Imagens/' . $imagem . '.jpg"></center><p>' . $coment . '</p><br><center>Postado : ' . date('d/m/y') . '</center><br><br></div>' ;

// abre o arquivo colocando o ponteiro de escrita no final
$arquivo = fopen('../../../HTML/noticias.php','a+');
if ($arquivo) {
    // move o ponteiro para o inicio do arquivo
    rewind($arquivo);
    if (!fwrite($arquivo, $finalcoment)) die('Não foi possível atualizar o arquivo.');
    echo 'Arquivo atualizado com sucesso';
    fclose($arquivo);
}

?>
<meta http-equiv="refresh" content="1; ../noticias.php">

In theory, it should take values from strings , put it inside the code to write at the beginning of the other .php file. He is writing the code all right but not at the beginning of the page, he is writing in continuation to the last typed code. Here comes the big question, what did I do wrong in the code?

    
asked by anonymous 08.10.2016 / 16:31

2 answers

1

According to the rewind documentation:

  

If you opened file in append mode ( a or a+ ), any   information you write to the file will always be added,   disregarding the position in the file.

If the file you want to manipulate is not too heavy, use the file_get_contents / a> to read the content and file_put_contents to write:

$finalcoment  = "<div id=.... \n";
$finalcoment .= file_get_contents('noticias.php');

file_put_contents('noticias.php', $finalcoment);

If you prefer to use the functions fopen , fwrite , in this answer has an implementation.

    
08.10.2016 / 17:16
1

This method worked well for file_get_contents and file_put_contents I just had to set $finalcoment to fit right on the page giving space on the top and bottom line, then it leaves a line always remaining for the next code that goes into this file. It looks like this:

<?php

$id = $_POST['id'];
$titulo = $_POST['titulo'];
$imagem = $_POST['imagem'];
$coment = $_POST['coment'];

$finalcoment = '
<div id="triplox"><a name="index-' . $id . '"></a><br><div id="idnoticias"><center><a href="HTML/Noticias/index-' . $id . '.php"><u>' . $titulo . '</u></a></center></div><br><br><center><img width="90%" height="90%" src="Imagens/' . $imagem . '.jpg"></center><p>' . $coment . '</p><br><center>Postado : ' . date('d/m/y') . '</center><br><br></div>
' ;

$finalcoment .= file_get_contents('noticias.php');

file_put_contents('noticias.php', $finalcoment);

echo "Noticia adicionada com sucesso!";
?>

<meta http-equiv="refresh" content="2; noticias.php">

In case I gave ENTER at the beginning and end of the code, inside the single quotation mark, it is added to the code together.

    
09.10.2016 / 17:01