FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Deleting certain lines from text file





Hobbit
Hopefully someone can help. I have a text file the contents are for example

line1
line2
line3

How can I make a php script (edit.php) that will make something like this

line1 - delete
line2 - delete
line3 - delete

I currently only have is so it shows the data of the text file but that's it.

Code:
<?php

// set file to read
$file = '/home/mypath/file.txt' or die('Could not open file!');
// open file
$fh = fopen($file, 'r') or die('Could not open file!');
// read file contents
$data = fread($fh, filesize($file)) or die('Could not read file!');
// close file
fclose($fh);
// print file contents
echo $data;

?>
mathiaus
Something like this for making that page;
Code:
<?php

// set file to read
$file = '/home/mypath/file.txt' or die('Could not open file!');
// open file
$fh = fopen($file, 'r') or die('Could not open file!');

// Loop through each line
$i = 0;
while (!feof($handle)) {
   $buffer = fgets($handle);
   echo $buffer . ' &nbsp; - &nbsp; <a href="delete.php?line='.$i.'">Delete</a>';
}

// close file
fclose($fh);
// print file contents
echo $data;

?>


I'm not really a 'file' person so I don't think my ideas about actually deleting the line are any good. I was thinking about reading a file line by line, storing into a variable, skipping the line to be deleted, then writing it all to the file. Sounds rather inefficent, I'm sure others here can think of something better!
sonam
I am not sure is this what do you want!

data.txt

Quote:
line1
line2
line3


edit.php
Code:
<?php
$textFile = file("data.txt");
foreach($textFile as $key => $val) {
    $line = @$line . $val . "<input type='submit' name=$key value='Delete'><br />";
}

$form = "<form name='form' method='post' action='deleter.php'> $line
</form>";

echo $form;
?>


deleter.php
Code:
<?php
$textFile = file("data.txt");     
     foreach($_POST as $key => $val) {
       if($val == "Delete") {       
             $lines = count($textFile);
             $textFile[$key] = "";
             $fileUpdate = fopen("data.txt", "wb");
             for($a=0; $a<$lines; $a++) {
                   fwrite($fileUpdate, $textFile[$a]);
             }
             fclose($fileUpdate);          
        }      
   }
   header("Location:edit.php");
   exit;        
?>


I know you dont need explains. Wink

Sonam
DanielXP
He wants a script on which it save in a txt file like this.

Line 1
Line 2
Line 3


Then he wants it to display on a php page like this.

Line 1 - Delete
Line 2 - Delete
Line 3 - Delete

And then when he clicks the delete it deletes that line.

So clicks on "Line 2 - Delete" and it deletes "Line 2"

I googled it and found this.

http://www.codewalkers.com/c/a/File-Manipulation-Code/delete-lines-from-a-file/

Not tested but it should work.

Good Luck
Daniel
sonam
Ah, my mistake. Embarassed

Code:
<?php
$delete = @$_GET['delete'];
$textFile = file("data.txt");
$lines = count($textFile);

if($delete != "" && $delete >! $lines || $delete === '0') {
    $textFile[$delete] = "";
    $fileUpdate = fopen("data.txt", "wb");
    for($a=0; $a<$lines; $a++) {
           fwrite($fileUpdate, $textFile[$a]);
    }
    fclose($fileUpdate);
   header("Location:edit.php");
   exit;
}

foreach($textFile as $key => $val) {
$line = @$line . $val . "<a href =?delete=$key> Delete </a><br />";
}
echo $line;
?>


Sonam
Hobbit
Thank you so much Sonam, that works really well.
Hobbit
One problem though but it has nothing to do with your code. When I submit the data to the text file, it's all in one lines so there's only one delete link.

Maybe I should use mysql but I wanted to do something really simple for starters. But do you know how to submit something into a text file and make it change lines? For example

1st submission
2nd submission
sonam
Quote:
Thank you so much Sonam, that works really well

I am happy if I can help. Laughing Laughing Laughing

It is quite simple to insert new data in new line but start from first line (do not insert nothing by hand).

Code:
<?php
$dataNew = @$_POST['yourdata'] . "\n"; // this "n" create always new line
    $fileUpdate = fopen("data.txt", "ab"); // "ab" insert cursor in last empty line
    fwrite($fileUpdate, $dataNew);
    fclose($fileUpdate);      
?>


Sonam
Hobbit
I see, thanks a lot I will try it!

edit: That worked. Thank you so much. Very Happy
DanielXP
I was going to reply yesterday but forgot what one it is.

\n or \r

What does the \r do?
sonam
Both \n or \r make new line. I think, but I am not 100% sure, \n is on Unix, Linux OS and \r on Win.

Sonam
Related topics
users online, using php and text file
Random Quotes Services for anyone - 1 Frih$ for 170+ quotes
Visual Basic Tutorial - File I/O in VB: Text Files
styling text string read from plain text file
Reading from text file into Flash
HELP, reading text file from USB drive in visual basic
download a text-file daily
how to add a text file in mp3 data using java ?
WINDOWS TRUE HIDDEN FILES
English Dictionary in text file or sql database
Perl how to encrypt your text file contents with Perl
Direct Admin editor appending text to XML and XSLT files
edit option not available for a text file
want to delete first 5 lines in a text file
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.