Hello everyone!

I have generated a txt file from the billing system. My problem is, whenever the transaction is cancelled the newly written data in the txt file must be erase. Is there a function that erases the content of the txtfile generated from the system? I have tried getting the data from the txt file. It works but it wasnt erase in the txt file. pls help me on to solve on this. If you have an alternative solution or suggestion i will surely appreciate it. Thank you very much.

Note: The purpose why i need to delete the content of the generated file is because other system taking data in the generated txt file and prevent delay in reading the txt file.

Here's a trick: Open that same text file in Create Mode. This will erase the text file and create a new one with that name....

Hope this works..

there is another way also
u can use

file_put_contents()

and insert an empty string

and also u can delete the file using

unlink

Hi! The file_put_contents() function works. thanks for helping me w_3rabi. I truly appreciate it. But in my case i dont have to delete the entire text, only the selected rows and recently entered data.

hussulinux, sorry but i dont have an idea with regards to your suggestion coz im new both to Php and MySQL. Pls give me the details or sample code on how to do it. Hope i am not giving you so much pain on this. Thanks a lot.

All of the solutions about delete the entie contents of the text file. Just to clear up hussulinux's point (as I think it is probably the most resource friendly, while still keeping the text file. Basically when you open a file in php you usually use either append mode or create mode. Append mode adds data to the end of the file and keeps the data already in the file unchanged
. Whereas create mode deletes the data in the file and then adds the new data. (I know that doesn't do what you want but as you said you didn't understand I thought I would clear it up!!)

Back to you problem. It is a little trick what you are trying to achieve in the way that you are try to achieve it.

As I understand your situation you have a function in you billing system that when transaction created you enter a record in you text file but you problem is that if it is cancelled then you need to remove the entry from the text file.

First you have to understand that a text file is in essance an unordered string of data (Don't all scream at me for saying that.)

The best way to achieve this is actually to place the transactions records in a database table, this will make removing the cancellations easy, quick and clean.

But you are using text files so. First make sure that each transaction record in you text file is on a new line. I assume that you have a transacion id for each transaction so that is in the same format each time. I am also going to assume that you have placed these first on the line and they are 6 characters long for ease of example.

What you have to do is lop through every line of text in the file and look for the transaction id, when you found it delete that line.
Example:

function cutline($filename,$trans_id) { 

//Count Delete Transactions
$removal_count=0;

//Read all file data into an array with on transaction per element
$data=file($filename);
 
//Open the File again in write mode
$pipe=fopen($filename,'w'); 

//Get the number of rows in the file
$size=count($data);
 
//Loop through the array
for($i=0;$i<$size;$i++)
{
   Check to see if this line is our transaction.

    (N.B. Could use strpos for varing length of trans_id but might has unexpected results, if the     trans_id string appears elsewhere it the row.)

   if(substr($data[$i],0,strlen($trans_id))  != $trans_id)
   {

     //write all data that is not the transaction we are looking for back to the file
     fputs($pipe,$data[$line]);
 
   }
   else
   {

    Count how many rows we discard so that we can have a test for action later
    $removal_count++;
    }
}return $removal_count;

}

Call the function like this
$number_deleted = cutline('foo.txt',"AAAAA1"); 

echo "I have removed $number_deleted transactions from the record";

This is a very quick and dirty function and can be done but better I am sure but I hope it will give you an undersatnding of how this could work.

Best Regards,

Urban

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.