Re: File handling
| From: | Justin French | Date: | Fri, 29 Nov 2002 12:22:36 +0000 |
| Subject: | Re: File handling | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-126467@lists.php.net to get a copy of this message | ||
on 29/11/02 7:06 PM, Jeff RingRose (JRingRose@atlasalu.ie) wrote:
> Justin,
> Option b. delete all AFTER "foo", I.E. truncate the file directly after
> "foo". Leaving "foo" and all the data before "foo" untouched.
And what happens if there are more than one occurrence of "foo"? I assume
you mean the first occurrence.
And what happens if "foo" is found within another word, like "aafooaa"...
this example assumes this doesn't matter... it's looking for 'foo', not '
foo '.
One of way is to read the file into a variable, split the var on "foo", and
rewrite the file out.
<?
$split = 'foo';
$file = 'dummyfiles/01-04-29.php';
// get file contents
$fd = fopen ($file, "r");
$mytext = fread ($fd, filesize($file));
fclose ($fd);
// split it on $split, append $split to first chunk
list($prefix,$suffix) = split($split,$mytext);
unset($suffix);
$newtext = $prefix.$split;
// write to file
$fp = fopen($file, 'w');
fwrite($fp, $newtext);
fclose($fp);
?>
1. Totally untested code, but most of it was lifted out of the manual in
some way or another.
2. Permissions of the file to be read/written will need to be correct
3. I've included no error reporting or correct checking... you'll need to
add this yourself
Season to taste...
Justin French
--------------------
http://Indent.com.au
Web Development &
Graphic Design
--------------------