Read and write in txt

Asked

Viewed 181 times

3

I have a simple code to read and write the txt made in PHP. But I don’t understand why it is increasing 3 by 3 every time I refresh the page. I ran a test on an empty file just with this code and it works.

Write down

$arquivo = "contador.txt";
$handle = fopen($arquivo, 'r+');
$data = fread($handle, 512);
$contador = $data + 1;
fseek($handle, 0);
fwrite($handle, $contador);
fclose($handle);

To read

$f = fopen("contador.txt", "r");
echo fgets($f); 
fclose($f);

I don’t know what it could be

Here the full page code.

<?php
  include_once("verifica.php");
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Painel de Administração</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/navbar-fixed-top.css" rel="stylesheet">
    <script src="sweet/sweetalert.min.js"></script>
    <link rel="stylesheet" type="text/css" href="sweet/sweetalert.css">
    <script src="ckeditor/ckeditor.js"></script>

  </head>

  <body>
<?php
$msg = $_GET['msg'];
if($msg == 'ok') { echo '<script type="text/javascript">swal({   title: "Sucesso",   text: "Imóvel Cadastrado com Sucesso",   type: "success",   timer:1500,   showConfirmButton: false });</script>'; }
if($msg == 'delok') { echo '<script type="text/javascript">swal({   title: "Sucesso",   text: "Imóvel Excluído com Sucesso",   type: "success",   timer:1500,   showConfirmButton: false });</script>'; }
if($msg == 'editok') { echo '<script type="text/javascript">swal({   title: "Sucesso",   text: "Imóvel Editado com Sucesso",   type: "success",   timer:1500,   showConfirmButton: false });</script>'; }
if($msg == 'fotook') { echo '<script type="text/javascript">swal({   title: "Sucesso",   text: "Fotos Cadastradas com Sucesso",   type: "success",   timer:1500,   showConfirmButton: false });</script>'; }
if($msg == 'delfotook') { echo '<script type="text/javascript">swal({   title: "Sucesso",   text: "Foto Excluída com Sucesso",   type: "success",   timer:1500,   showConfirmButton: false });</script>'; }

?>
    <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        <a class="navbar-brand" style="padding-top:-20px;" href="index.php"><img src="imagens/logo2.png" ></a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="index.php">Home</a></li>
            <li><a href="cad_imovel.php">Cadastrar Imóvel</a></li>

            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Cadastar Opções <span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="tipos.php">Tipos de Imóvel</a></li>
                <li><a href="cidades.php">Cidades</a></li>
                <li><a href="bairros.php">Bairros</a></li>
                <li><a href="banners.php">Banners</a></li>
              </ul>
            </li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="sair.php">Sair</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
    <?php
   include('conecta.php');

    $query=mysql_query("SELECT * FROM imoveis ORDER BY id DESC");
    $total = mysql_num_rows($query);
    ?>
    <div class="container">
      <div class="jumbotron">
      <h4>Total de visitas no site: <span class="label label-info">
        <?php
          $f = fopen("../contador.txt", "r");
          echo fgets($f); 
          fclose($f);
        ?>
      </span></h4>
        <h4>Todos os imóveis <span class="badge"><?php echo $total; ?></span></h4>
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>Nome</th>
              <th class="col-md-1">Cidade</th>
              <th class="col-md-1">Operação</th>
              <th class="col-md-1">Ativo?</th>
              <th class="col-md-1">Visitas</th>
              <th class="col-md-2 text-center">Ação</th>
            </tr>
          </thead>
          <tbody>
            <?php
            while($ver=mysql_fetch_array($query)){
                ?>
            <tr>
            <td><?php echo $ver['nome']; ?></td>
            <td><?php echo $ver['cidade']; ?></td>
            <td><?php echo $ver['operacao']; ?></td>
              <td class="text-center">
              <?php if ($ver['ativo'] == sim){ echo "<span class=\"label label-success\">SIM</span>"; }else{ echo "<span class=\"label label-danger\">NÃO</span>"; } ?>
              </td>
              <td class="text-center"><?php echo $ver['visitas']; ?></td>
              <td class="text-center">
                <a class="btn btn-primary btn-xs" data-toggle="modal" data-target="#MandaFotos<?php echo $ver['id']; ?>"><span class="glyphicon glyphicon-picture" aria-hidden="true"></span></a>
                <a class="btn btn-warning btn-xs" data-toggle="modal" data-target="#EditaImovel<?php echo $ver['id']; ?>"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
                <a class="btn btn-danger btn-xs"  data-toggle="modal" data-target="#DeleteImovel<?php echo $ver['id']; ?>"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
              </td>
            </tr>

            <!-- Modal Editaimovel-->
            <div class="modal fade bs-example-modal-lg" id="EditaImovel<?php echo $ver['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel<?php echo $ver['id']; ?>" aria-hidden="true">
              <div class="modal-dialog modal-lg">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel<?php echo $ver['id']; ?>">Editar Imóvel</h4>
                  </div>
                  <div class="modal-body">

                    <form action="edita_imovel.php" method="post" enctype="multipart/form-data" name="cadastro" >
                      <div class="form-group">
                        <label for="exampleInputEmail1">Nome:</label>
                        <input type="text" class="form-control" name="nome" id="nome" value="<?php echo $ver['nome']; ?>">
                      </div>
                  <div class="form-group">
                  <label>Tipo: </label>
                    <select name="tipo" id="tipo" class="form-control">
                      <?php echo "<option value=\"".$ver['tipo']."\">".$ver['tipo']."</option>"; ?>
                    <option>--------------</option>
                    <?php 
                      $querytipos=mysql_query("SELECT tipo FROM tipos ORDER by tipo ASC");
                      while($vertp=mysql_fetch_array($querytipos)){
                        echo "<option value=\"".$vertp['tipo']."\">".$vertp['tipo']."</option>";
                      }
                    ?>
                    </select>
                  </div>
                  <div class="form-group">
                  <label>Operação: </label>
                    <select name="operacao" id="operacao" class="form-control">
                      <?php echo "<option value=\"".$ver['operacao']."\">".$ver['operacao']."</option>"; ?>
                    <option>--------------</option>
                    <option>Aluguel</option>
                    <option>Venda</option>
                    </select>
                  </div>

                      <div class="form-group">
                        <label for="exampleInputPassword1">Descrição:</label>
                        <textarea class="form-control" rows="3" name="descricao" id="descricao<?php echo $ver['id']; ?>"><?php echo $ver['descricao']; ?></textarea>
                      </div>
                  <div class="form-group">
                        <label for="exampleInputEmail1">Endereço:</label>
                        <input type="text" class="form-control" name="endereco" id="endereco" value="<?php echo $ver['endereco']; ?>">
                      </div>
                  <div class="form-group">
                  <label>Cidade: </label>
                    <select name="cidade" id="cidade" class="form-control">
                      <?php echo "<option value=\"".$ver['cidade']."\">".$ver['cidade']."</option>"; ?>
                    <option>--------------</option>
                    <?php 
                      $querycidades=mysql_query("SELECT cidade FROM cidades ORDER by cidade ASC");
                      while($verci=mysql_fetch_array($querycidades)){
                      echo "<option value=\"".$verci['cidade']."\">".$verci['cidade']."</option>";
                      }
                    ?>
                    </select>
                  </div>                  
                  <div class="form-group">
                  <label>Bairro: </label>
                    <select name="bairro" id="bairro" class="form-control">
                      <?php echo "<option value=\"".$ver['bairro']."\">".$ver['bairro']."</option>"; ?>
                    <option>--------------</option>
                    <?php 
                      $querybairros=mysql_query("SELECT bairro FROM bairros ORDER by bairro ASC");
                      while($verbr=mysql_fetch_array($querybairros)){
                      echo "<option value=\"".$verbr['bairro']."\">".$verbr['bairro']."</option>";
                      }
                    ?>
                    </select>
                  </div>
                  <div class="form-group">
                  <label for="exampleInputEmail1">Tamanho:</label>
                  <input type="text" class="form-control" name="tamanho" id="tamanho" value="<?php echo $ver['tamanho']; ?>">
                  </div>  

                    <div class="form-group">
                              <label>Quartos: </label>
                                <select name="quartos" id="quartos" class="form-control" required>
                                  <option value="<?=$ver['quartos']?>"><?=$ver['quartos']?></option>
                                  <option>--------------</option>
                                  <option value="0">0</option>
                                  <option value="1">1</option>
                                  <option value="2">2</option>
                                  <option value="3">3</option>
                                  <option value="4">4</option>
                                  <option value="5">5</option>
                                  <option value="6">6</option>
                                  <option value="7">7</option>
                                  <option value="8">8</option>
                                  <option value="9">9</option>
                                  <option value="10">10</option>
                              </select>

                              <label>Banheiros: </label>
                                <select name="banheiros" id="banheiros" class="form-control" required>
                                  <option value="<?=$ver['banheiros']?>"><?=$ver['banheiros']?></option>
                                  <option>--------------</option>                                
                                  <option value="0">0</option>
                                  <option value="1">1</option>
                                  <option value="2">2</option>
                                  <option value="3">3</option>
                                  <option value="4">4</option>
                                  <option value="5">5</option>
                                  <option value="6">6</option>
                                  <option value="7">7</option>
                                  <option value="8">8</option>
                                  <option value="9">9</option>
                                  <option value="10">10</option>
                              </select>        
                    </div>  

                  <div class="form-group">
                        <label for="exampleInputEmail1">Valor:</label>
                        <input type="text" class="form-control" name="valor" id="valor" value="<?php echo $ver['valor']; ?>">
                      </div>
                  <br>
                          <label>Ativo: </label>
                            <label class="radio-inline">
                              <input type="radio" id="ati1" name="ativo" value="sim" <?php if($ver['ativo'] == "sim"){ echo "checked"; } ?>> SIM
                            </label>
                            <label class="radio-inline">
                              <input type="radio" id="ati2" name="ativo" value="nao" <?php if($ver['ativo'] == "nao"){ echo "checked"; } ?>> NÃO
                            </label>
                  <br><br>

                  <label>Foto de Capa: </label>
                  <img class="borda1" src="../fotos/<?php echo $ver['foto']; ?>" width="150" height="100">
                  <input type="file" id="foto" name="foto" class="form-control">

                  <br><br>

                    <input type="hidden" name="id" value="<?php echo $ver['id']; ?>">
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                    <button type="submit" class="btn btn-primary">Salvar</button>
                  </div>
                  </form>
                </div>
              </div>
            </div>

            <!-- Modal Deleta-->
            <div class="modal fade" id="DeleteImovel<?php echo $ver['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel<?php echo $ver['id']; ?>" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel<?php echo $ver['id']; ?>">Deletar Imóvel</h4>
                  </div>
                  <div class="modal-body">

                    <div class="form-group">
                        <form action="del_imovel.php" method="post">
                                Tem certeza que deseja deletar o imóvel <b><?php echo $ver['nome']; ?></b>?
                                  <input type="hidden" name="id" value="<?php echo $ver['id']; ?>">
                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                    <button type="submit" class="btn btn-primary">Confirmar</button>
                  </div>
                  </form>
                </div>
              </div>
            </div>

            <!-- Modal MandaFotos-->
            <div class="modal fade bs-example-modal-lg" id="MandaFotos<?php echo $ver['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel<?php echo $ver['id']; ?>" aria-hidden="true">
              <div class="modal-dialog modal-lg">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel<?php echo $ver['id']; ?>">Enviar fotos do imóvel</h4>
                  </div>
                  <div class="modal-body">

                    <div class="form-group">
                                Cadastrando fotos para o imóvel <b><?php echo $ver['nome']; ?></b><br><br>

                 <form action="cad_fotos.php" method="POST" enctype="multipart/form-data">

                 <input type="file" name="files[]" class="form-control" multiple/>
                 <center><button type="submit" class="btn btn-success">Enviar Fotos</button></center>
                 <input type="hidden" name="id" value="<?php echo $ver['id']; ?>">
                 </form>
<br><br><br>


Clique em cima da imagem para deletar.<br><br>
                 <?php

                 $queryfotos=mysql_query("SELECT * FROM fotos WHERE id_foto={$ver['id']} ORDER by nome ASC");
                 while($foto=mysql_fetch_array($queryfotos)){

                  echo "<a href=\"del_foto.php?id={$foto['id']}&nome={$foto['nome']}\"><img class=\"borda1\" src=\"../fotos/{$foto['nome']}\" width=\"100\" height=\"100\"></a>&nbsp;&nbsp;";
                 }
                 ?>

                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                  </div>
                </div>
              </div>
            </div>


                    </td>
                  </tr>
                <script>
                  CKEDITOR.replace( 'descricao<?php echo $ver['id']; ?>' );
                </script>
<?php } ?>
          </tbody>
        </table>

      </div>
    </div> <!-- /container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/vendor/jquery.min.js"><\/script>')</script>
    <script src="js/bootstrap.min.js"></script>

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="js/ie10-viewport-bug-workaround.js"></script>
  </body>
</html>
  • 4

    The code is correct, I tested it here and it’s incrementing from one to one. What version of PHP are you using? Has a version that has a bug that Repere several times an operation, may be your case.

  • I’m using the 5.5.36. tbm tested here on a soh page with this code. and it works. but when I enter in my PHP page, soh the command to read the txt and display. ja is increasing 3 by 3

  • So the problem is with this PHP page of yours. Add its code to your question.

  • ok. to adding

  • and where you call the function that increments the counter?

  • This is the problem. this page is soh to display. not to increment. and ta incrementing

Show 1 more comment
No answers

Browser other questions tagged

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