-
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?
-
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);
-
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? :confused:
-
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