Doubt about PHP command to write inside another. php file

Asked

Viewed 164 times

2

I’m finding 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 he should take the values of strings, mount it inside the code to write at the beginning of the other file .php. 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 I did wrong in the code?

2 answers

1


According to the function documentation rewind:

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

If the file you want to handle is not too heavy, use the functions file_get_contents 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 that reply has an implementation.

1

It worked well this method of file_get_contents and file_put_contents I just had to fix the $finalcoment to fit right into the page giving space on the top and bottom line, then it leaves a line always left to the next code that will enter this file. Stayed 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 a ENTER at the beginning and at the end of the code, inside the simple quotes, there it is added to the code together.

Browser other questions tagged

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