TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-10-2007, 11:07 PM   #1 (permalink)
The Visitor
 
Join Date: Apr 2007
Location: California
Posts: 4
Thanks: 0
Hobbit is on a distinguished road
Default 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.
Hobbit is offline  
Reply With Quote
Old 09-13-2007, 08:39 AM   #2 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
jordie is on a distinguished road
Default

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";

jordie is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:50 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design