TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Delete lines from txt file (http://www.talkphp.com/absolute-beginners/559-delete-lines-txt-file.html)

Hobbit 06-10-2007 11:07 PM

Delete lines from txt file
 
Hopefully someone can help. I have a text file the data is for 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;

?>

I'm making a link script. Basically for lazy people like me who are too lazy to upload the sidebar everytime. I finally got it to show the live links, but I want to be able to delete links as well. Even better, I'd like to move links up and down the list but I will wait till later for that. Any help would be greatly appreciated.

jordie 09-13-2007 08:39 AM

I suppose you could use PHP's file() function which reads in a file into an array with each line being a new element of the array.

PHP Code:

$file_array file('/home/mypath/file.txt');

foreach (
$file_array as $line => $value) {
    echo 
$value "<a href='url'>Delete</a><br />\n";


To be honest I would recommend using a database rather than this. Because the only way I can see here would be to delete the items based upon their line number or value. Both present issues. If you use the line number and someone else has edited the file in between the time you loaded the page and clicked delete, you could end up deleting a different line. The value could also potentially not be unique, so selecting to delete a line that has another the same would result in them both being deleted, though I think this one is your safer bet.

So overall, what you could do is this:

PHP Code:

$file_name '/home/mypath/file.txt';

// this reads the file into an array
$file_array file($file_name);

if(
$_GET['action'] == 'deleteline'){
    
// a delete request was made, unencode the data
    
$delete_value base64_decode($_GET['line']);

    
// loop through the file array and only include
    // the lines that aren't the one we want to delete into
    // a  new array
    
foreach ($file_array as $line => $value) {
        if(
$delete_value != $value){
            
$new_array[] = $value;
        }
    }
    
// replace the old array with our new one with a
    // line removed
    
$file_array $new_array;
    
    
// collapse the array to a string
    
$file_data implode('',$file_array);

    
// write the data back to the file
    
$handle fopen($file_name'w+');
    
fwrite($handle$file_data);
    
fclose($handle);
}

reset($file_array);

foreach (
$file_array as $line => $value) {
    echo 
$value "<a href='".$_SERVER['PHP_SELF']."?action=deleteline&line=".base64_encode($value)."'>Delete</a><br />\n";




All times are GMT. The time now is 05:34 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0