Problems with file_put_contents()

Asked

Viewed 384 times

1

I have a script that runs every time a particular page is loaded.

Basically, it does a database search and saves the values in a file:

$servicos = $db->prepare('SELECT id_servico, UNIX_TIMESTAMP(data) dia, user, cliente, situacao FROM servicos);
$servicos->execute(array());
$servicos_result = $servicos->fetchAll(PDO::FETCH_OBJ);

$out = array();
foreach($servicos_result as $row) {
    $out[] = array(
        'id' => $row->id_servico,
        'user' => $row->loja,
        'cliente' => $row->cliente,
        'situacao' => $row->situacao,
        'start' => $row->dia . '000',
        'end' => $row->dia . '000'
    );
}

$stringData = json_encode(array('success' => 1, 'result' => $out));
file_put_contents ( "events.json.php" , $stringData );

The problem is that it has taken a while for the file to be updated after some change in the bank. That is: it is not updated the first time the page is loaded, but only after a few minutes and attempts.

The file is about 400kb.

This may be some server limitation?

  • 1

    Write permission in directory?

  • @Shutupmagda. The file has 666 permission. The information is saved, but it takes too long.

  • What is the time of completion of the consultation itself?

  • @Smael By The Workbench is being at 0.078 sec / 0.282 sec

  • 1

    @marcelo2605 I deleted my previous comments to not leave mess under your question, as it is already solved. Any new questions, open a question, or if you have questions about this one, leave a comment on the answer I’ll return if you are online.

1 answer

3


If you don’t really need to make a disk cache, it’s much easier to use a simple echo, and write the file itself as events.json.php :

$servicos = $db->prepare('SELECT id_servico, UNIX_TIMESTAMP(data) dia, user, cliente, situacao FROM servicos);
$servicos->execute(array());
$servicos_result = $servicos->fetchAll(PDO::FETCH_OBJ);

$out = array();
foreach($servicos_result as $row) {
    $out[] = array(
        'id' => $row->id_servico,
        'user' => $row->loja,
        'cliente' => $row->cliente,
        'situacao' => $row->situacao,
        'start' => $row->dia . '000',
        'end' => $row->dia . '000'
    );
}

echo json_encode(array('success' => 1, 'result' => $out));

It would make sense for you to write to a file if it was a very widely accessed application, and little changed, but for smaller volumes of access, or larger of change, serving the data directly from DB is not a problem.

But note that if you use this strategy in the future, save to DB, avoid the extension .php, otherwise it will pass the parser of PHP for no reason.

In your case, apparently the delay was in the browser cache, not in the recording of the file. One solution would be to set up a folder on the web server with a completely different cache rule, but if the above solution is sufficient, it is much more practical.

Browser other questions tagged

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