|
-
Sep 22nd, 2005, 02:35 PM
#1
Thread Starter
Frenzied Member
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.
-
Sep 23rd, 2005, 11:02 AM
#2
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);
-
Sep 23rd, 2005, 06:07 PM
#3
Thread Starter
Frenzied Member
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?
-
Sep 23rd, 2005, 06:31 PM
#4
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|