Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:23:42+00:00 2026-06-15T13:23:42+00:00

Hi all am new to the php ,I try out file handling in php

  • 0

Hi all am new to the php ,I try out file handling in php , i.e., storing the information to the text files not in the database .which is basically insert the data to the text files and deleting the data in a file but i am struggling to delete the last records in the text file here is my code anyone much appreciated..

    <html>
<head>
<title>
Registration
</title>
<style type="text/css">
table{font-family:calibri;color:black;font-size:11pt,font-style:normal;background-color:white;border-collapse:collapse;border:2px solid navy}
</style>
<script type="text/javascript" src="frnt.js">
</script>
</head>
<body bgcolor="aqua">
<form name="myform" action="writ.php" method="post" onsubmit="return validation()">
<table border="0px"align="center" cellpadding="5" cellspacing="5">
<tr>
<th colspan="2" align="center">Registration</th>
</tr>
<tr>
<td>UserName:</td>
<td><input type="text" name="nme" maxlength='20'>
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="mail" maxlength='20'>
</td>
</tr>
<tr>
<td>Mobile:</td>
<td><input type="text" name="mobile" maxlength='10'>
</td>
</tr>
<tr>
<td>Education:</td>
<td><input type="text" name="edu" maxlength='10'>
</td>
</tr>
<tr>
<td>College:</td>
<td><input type="text" name="clg" maxlength='15' >
</td>

</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit" name="submit"><input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>

And this page redirects to the writ.php and this code is to be

<html>
    <body>
    <form method = "post">
        <?php

        $body = $_POST['nme'];
        $body1 = $_POST['mail']; 
        $body2 = $_POST['mobile'];
        $body3 = $_POST['edu'];
        $body4 = $_POST['clg'];
        $file_name = "test.txt";               
        $fp = fopen ($file_name, "a"); 

        fwrite ($fp,"Name:".$body."\r");
        fwrite ($fp,"Email:".$body1."\r");
        fwrite ($fp,"Mobile:".$body2."\r");
        fwrite ($fp,"Education:".$body3."\r");
        fwrite ($fp,"College:".$body4."\r\r");


            if(fwrite)  {
                echo "your data added successfully <br><br>";
                echo "<a href='back .php'>Go back to view your page</a>";
            }

        ?>
    </form>
    </body>
    </html>

And this is deleting whole data’s in the text file

<?php
$v=filesize("test.txt");
echo "<br />";

$file = fopen("test.txt", "a+");
$d=ftruncate($file,0) or die ('could not open'); 
if($d)  {
echo "deleted the whole data's successfully<br><br>";
echo"<a href='frm.php'>Add data to a file </a><br><br>";
echo"<a href='test.txt'>View the file </a><br><br>";
}
fclose($file);
?>

Finally i have an problem with the this coding

<?php 
$file="test.txt";
$str = fopen("test.txt","r");
$s = fgets($str);

$parts = explode("\r",$s);
$content = file_get_contents($file);
$c=count($parts);
$dd=$c-8;
for($i=$c;$i>$dd;$i--)  {

$content = str_replace($parts[$i],'', $content);
/*$content = str_replace($parts[7],'', $content);
$content = str_replace($parts[8],'', $content);
$content = str_replace($parts[9],'', $content);
$content = str_replace($parts[10],'', $content);
*/
$fh = fopen($file, 'w');
$d=fwrite($fh, $content);
}
//unset($parts[$i],'');
if($d)  {
echo"Last data deleted successfully <br><br>";
}

?>

In the above coding i face the problem has when i enter the same email id to the multiple times its store in the text file but when i going to delete the last records in the file the same name of email also deleted !!!! And how to solve this .
Please help! Much appreciated:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:23:44+00:00Added an answer on June 15, 2026 at 1:23 pm

    if you change fopen("test.txt", "a")to fopen("test.text", "w") it will delete everything that is inside the txt.
    “w” means: Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. (According to PHP.net fopen)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to PHP first of all .. My question is once I got
I am really new with PHP and all I have to do is to
I'm new to MySQL and PHP. I have two tables, one to hold all
Hi I'm calling out for help from all the PHP Gods on Stackoverflow :)
my sample code is here include 'simple_html_dom.php'; function get_all_links($url){ global $host; $html = new
<?php ini_set('display_errors',1); error_reporting(E_ALL); require_once('/include/adLDAP.php'); $adldap = new adLDAP(); $username = user123; $password = pass123;
im all new to framework. so the structure of code igniter looks like: system
How would you normalize all new-line sequences in a string to one type? I'm
What I want: Update all new commits from server with my local repository in
We are trying to get the Model-View-Presenter pattern used on (virtually) all new dev

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.