How to delete specific contents of a . txt with php

Asked

Viewed 129 times

-1

How can I delete a line from a specified . txt file in a PHP input

I wanted to put on the admin panel

My Admin Panel code

<!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">
        <link rel="icon" type="imagem/png" href="img/icon.png" />

        <script language=javascript>
            senha = 'plsfodas';
            senhadig = prompt("Digite a senha","")
            if (senha != senhadig){
                alert('Acesso negado!');
                top.location.href='index.php';
            }
        </script>
        <title>Painel De Controle</title>

        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

        <script>
            $(function() {
                var wbbOpt = {
                    buttons: "bold,italic,underline,|,img,link,|,code,quote"
                }
                $("#link").wysibb(wbbOpt);
            })
        </script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <header class="col-sm-12">
                    <h1>Painel de Controle</h1>
                    <p><a href="plugins.php" class="btn btn-info"><i class="fa fa-arrow-left"></i> Plugins</a></p><br/>
                </header>

                <div id="content-form" class="col-sm-12">
                    <?php
                    if(isset($_POST['plugin']) && isset($_POST['valor']) && isset($_POST['link']) && isset($_POST['version'])) {


                        $plugin = $_POST['plugin'];
                        $valor = $_POST['valor'];
                         $link = $_POST['link'];
                         $version = $_POST['version'];

                        $news = unserialize(file_get_contents('news.txt'));
                        $news[] = array('plugin' => $plugin, 'version' => $version, 'valor' => $valor, 'link' => $link);
                        file_put_contents('news.txt', serialize($news));

                        echo '<div class="alert alert-dismissible alert-success"><h4><i class="fa fa-check"></i> Concluido</h4><p>Informações adicionadas';
                        echo '<br/>';
                        echo '<a href="plugins.php" class="alert-link">Visualizar</a></p></div>';
                    }
                    else {
                    ?>
                    <form action="" class="form-horizontal" method="post">

                         <div class="form-group">
                            <label class="col-sm-2 control-label" for="plugin">Nome do Plugin</label>
                            <div class="col-sm-10">
                                <input class="form-control" type="text" name="plugin" id="plugin" placeholder="Plugin" autofocus required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="version">Versão</label>
                            <div class="col-sm-10">
                                <input class="form-control" type="text" name="version" id="version" placeholder="Version" autofocus required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="valor">Valor</label>
                            <div class="col-sm-10">
                                <input class="form-control" type="text" name="valor" id="valor" placeholder="valor" autofocus required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="link">Link</label>
                            <div class="col-sm-10">
                                <input class="form-control" type="text" name="link" id="link" placeholder="Link" autofocus required>
                            </div>
                        </div>



                        <button class="btn btn-primary pull-right" type="submit"><i class="fa fa-plus"></i>Adicionar</button>
                    </form>
                    <?php
                    }
                    ?>
                </div>

                <footer class="col-sm-12">
                    <p><i class="fa fa-copyright"></i> Desenvolvido por Wicaro <a</p>
                </footer>
            </div>
        </div>    
    </body>
</html>

Content of . txt looks like this

a:2:{i:0;a:4:{s:6:"plugin";s:6:"tester";s:7:"version";s:5:"teste";s:5:"valor";s:5:"teste";s:4:"link";s:5:"teste";}i:1;a:4:{s:6:"plugin";s:7:"tester2";s:7:"version";s:6:"teste2";s:5:"valor";s:6:"teste2";s:4:"link";s:6:"teste2";}}

(Has 2 content added there)

2 answers

1

I made a code using an ajax function passing the information in json format to the action_plugins.php file, which receives the input data and saves in file called 'plugin.txt'.

I also checked if the file exists, if it does not exist, if it saves the information in the file overwriting the previous data (so I understand you want to update the version of the plugin). Create the file 'plugin.txt' so that the codes below run smoothly. Follow the code:

index.html:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
        crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
        <script type="text/javascript">
            $(document).ready(function ()
            {
                $('form').submit(function (e) {
                    e.preventDefault();
                })

                $("#enviar").click(function () {
                    let dadosPlugin = {};

                    dadosPlugin.plugin = $("#plugin").val();
                    dadosPlugin.version = $("#version").val();
                    dadosPlugin.valor = $("#valor").val();
                    dadosPlugin.link = $("#link").val();
                    $.ajax({
                        type: 'POST',
                        url: 'action_plugins.php',
                        data: dadosPlugin,
                        dataType: 'json',
                        success: function (resposta) {
                            console.log(resposta);
                            $("#resp").attr('style', 'color:blue');
                            $("#resp").html("<h6>Concluido!!!</h6>Informações adicionadas:<div style='color:black'>" + "Plugin: " + resposta.plugin + "<br>Version: " + resposta.version + "<br>Valor: " + resposta.valor + "<br>Link: " + resposta.link + "</div>");
                        },
                        error: function (erro) {
                            console.log(erro);
                            $("#resp").attr('style', 'color:red');
                            $("#resp").html(erro);
                        }
                    })
                })
            });
        </script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <header class="col-sm-12">
                    <h1>Painel de Controle</h1>
                    <p><a href="plugins.php" class="btn btn-info"><i class="fa fa-arrow-left"></i> Plugins</a></p><br/>
                </header>

                <div id="content-form" class="col-sm-12">
                    <?php
                    require 'action_plugins.php';
                    ?>
                    <form class="form-horizontal" method="post">

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="plugin">Nome do Plugin</label>
                            <div class="col-sm-10">
                                <input class="form-control" type="text" name="plugin" id="plugin" placeholder="Plugin" autofocus required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="version">Versão</label>
                            <div class="col-sm-10">
                                <input class="form-control" type="text" name="version" id="version" placeholder="Version" autofocus required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="valor">Valor</label>
                            <div class="col-sm-10">
                                <input class="form-control" type="text" name="valor" id="valor" placeholder="valor" autofocus required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="link">Link</label>
                            <div class="col-sm-10">
                                <input class="form-control" type="text" name="link" id="link" placeholder="Link" autofocus required>
                            </div>
                        </div>



                        <button class="btn btn-primary pull-right" type="submit" id='enviar' name="enviar"><i class="fa fa-plus"></i>Adicionar</button>
                    </form>

                    <span id="resp"></span>
                </div>

                <footer class="col-sm-12">
                    <p><i class="fa fa-copyright"></i> Desenvolvido por Wicaro <a</p>
                </footer>
            </div>
        </div>    
    </body>
</html>

action_plugins.php

<?php

if (isset($_POST['plugin']) && isset($_POST['valor']) && isset($_POST['link']) && isset($_POST['version'])) {


    $plugin = $_POST['plugin'];
    $valor = $_POST['valor'];
    $link = $_POST['link'];
    $version = $_POST['version'];


    $arq = "plugin.txt";
    if (!file_exists($arq)) {
        echo "Arquivo $arq não existe, não foi possível atualizar o plugin!";
        exit();
    } else {
        $news = array('plugin' => $plugin, 'version' => $version, 'valor' => $valor, 'link' => $link);
        file_put_contents('plugin.txt', serialize($news));
    }

    echo json_encode($news);
}
  • 1

    Thanks, manage to use elsewhere

0

The txt is actually the result of serialize of an array, so you can simply make an unset().

$news = unserialize(file_get_contents('news.txt'));
unset($news[0]);
file_put_contents('news.txt', serialize($news));

The unset($news[0]) will remove the item 0 of $news, and then will store this new set in the file again. Remembering that this does not reorder, then the $news[0] will not exist again, so the first item becomes the $news[1].

  • It worked as I can do for when I click on delete, the page of a Reload and appears a text talking that has been deleted successfully

  • @Markz your question is just how to delete the line, and that’s what I answered. Implementing it the way you want it is on your part.

Browser other questions tagged

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