Results 1 to 4 of 4

Thread: removing from file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    removing from file

    The code below adds text to the file in the format of:

    www.my-site.com/filename.php?data=hello

    PHP Code:
    <?php
    $filename 
    'test.txt';
    $somecontent $_GET['data'] . "\n"

    if (
    is_writable($filename)) {

       if (!
    $handle fopen($filename'a')) {
             echo 
    "Cannot open file ($filename)";
             exit;
       }

       if (
    fwrite($handle$somecontent) === FALSE) {
           echo 
    "Cannot write to file ($filename)";
           exit;
       }
       
       echo 
    "Success, wrote ($somecontent) to file ($filename)";
       
       
    fclose($handle);

    } else {
       echo 
    "The file $filename is not writable";
    }
    ?>
    but then how can i make a php file which will allow me to remove the word 'hello' from test.txt

    www.my-site.com/remove.php?data=hello
    any help on this please?
    Last edited by Pouncer; Sep 22nd, 2005 at 06:40 PM.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: removing from file

    Please don't post duplicates in more than one forum. I've deleted the thread you posted in General PC.

    The easiest way to remove your stirng is to load the file into a string and use the str_replace() function:
    PHP Code:
    $file file_get_contents($filename);

    $file str_replace("$somecontent\n"''$file);

    file_put_contents($file); 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: removing from file

    thanks i used

    PHP Code:
    <?php
    $filename 
    'test.txt';
    $somecontent $_GET['data'] . "\n"

    $file file_get_contents($filename); 

    $file str_replace("$somecontent\n"''$file); 

    file_put_contents($file);
    ?>
    but its now removing the word 'hello' from the test.txt

    im doing

    www.my-site.com/remove.php?data=hello

    which should remove that line, any ideas where im going wrong?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: removing from file

    error is:

    Fatal error: Call to undefined function: file_put_contents() in /home/pouncer/public_html/remove.php on line 8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width