Fwrite to write data in middle of page?
Moderator: General Moderators
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Fwrite to write data in middle of page?
I have a fwrite page, and I need it to write data in a specific part of the page. Is this possible?
Thank You!!!
Thank You!!!
Re: Fwrite to write data in middle of page?
Yep. Read in the file, split it (split(), preg_split(), substr()), and write to file: part1+additional_content+part2. Remember to fopen your file with fopen('dir/to/file', 'w').
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
read the file into an array with:
http://www.php.net/file
after you have it in use array_splice
http://www.php.net/array_splice
you will want to split it on the 66th key of the array, then use http://www.php.net/array_push to push an element onto the end of the first array then join them back together.
Then erase the existing file and use a foreach loop to write one line into the file at a time, alternatively http://www.php.net/implode the array using "\n" as the joining character
http://www.php.net/file
after you have it in use array_splice
http://www.php.net/array_splice
you will want to split it on the 66th key of the array, then use http://www.php.net/array_push to push an element onto the end of the first array then join them back together.
Then erase the existing file and use a foreach loop to write one line into the file at a time, alternatively http://www.php.net/implode the array using "\n" as the joining character
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
You might also want to look at fseek() to move the file pointer whilst you have the file open 
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
function insert_line($file, $text, $line_number=0) //If line number is 0 insert at end of file
{
$lines = file($file); //Array of lines
$handle = fopen($file, 'w+'); //Open for writing, create if not found
if ($line_number > 0) //Add line at line_number
{
$tmp = array(); //Temporary stack
for ($i = 0; $i < $line_number-1; $i++) //Copy data until the line given
$tmp[] = $lines[$i];
$tmp[] = $text; //Add line at correct point
for ($i = $line_number; $i < count($lines); $i++) //Copy remaining lines
$tmp[] = $lines[$i];
$lines = $tmp; //Copy the new stack back to the original
}
else $lines[] = $text; // ...or add line at end
$data = impode("\n", $lines); //Use \r\n on windows servers
fwrite ($handle, $data);
fclose($handle);
}- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
ok so basically my script would look like this..
Code: Select all
$data = $content;
function insert_line($file, $text, $line_number=63) //If line number is 0 insert at end of file
{
$lines = file($file); //Array of lines
$handle = fopen($file, 'w+'); //Open for writing, create if not found
if ($line_number > 0) //Add line at line_number
{
$tmp = array(); //Temporary stack
for ($i = 0; $i < $line_number-1; $i++) //Copy data until the line given
$tmp[] = $lines[$i];
$tmp[] = $text; //Add line at correct point
for ($i = $line_number; $i < count($lines); $i++) //Copy remaining lines
$tmp[] = $lines[$i];
$lines = $tmp; //Copy the new stack back to the original
}
else $lines[] = $text; // ...or add line at end
$data = impode("\n", $lines); //Use \r\n on windows servers
fwrite ($handle, $data);
fclose($handle);
}- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
$file = './foo.txt';
$new_line = 'This line will be inserted';
insert_line($file, $new_line, 68); //Insert at line 68- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
thank you for helping me, but i cant figure this out, i am not that good with php yet, i am still learning.
how would i put that into the code you provided me, i tried myself but i get parse errors. thank you for your time!
Code: Select all
$content ='I want this written on line 66';
$filepath = '/home/muot/public_html/pages/report.php';- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
$content ='I want this written on line 66';
$filepath = '/home/muot/public_html/pages/report.php';
insert_line($filepath, $content, 66);Corrected
Code: Select all
function insert_line($file, $text, $line_number=0) //If line number is 0 insert at end of file
{
$lines = file($file); //Array of lines
$handle = fopen($file, 'w+'); //Open for writing, create if not found
if ($line_number > 0) //Add line at line_number
{
$tmp = array(); //Temporary stack
for ($i = 0; $i < $line_number-1; $i++) //Copy data until the line given
$tmp[] = $lines[$i];
$tmp[] = $text."\n"; //Add line at correct point
for ($i = $line_number; $i < count($lines); $i++) //Copy remaining lines
$tmp[] = $lines[$i];
$lines = $tmp; //Copy the new stack back to the original
}
else $lines[] = "$text\n"; // ...or add line at end
$data = impode("", $lines);
fwrite ($handle, $data);
fclose($handle);
}- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
wait, so now i just put
in my processor page?
thank you for helping me!
oh yeah one other thing, i get parse errors if i have lines indented, any way to fix that?
Code: Select all
$content ='I want this written on line 66';
$filepath = '/home/muot/public_html/pages/report.php';
insert_line($filepath, $content, 66);
function insert_line($file, $text, $line_number=0) //If line number is 0 insert at end of file
{
$lines = file($file); //Array of lines
$handle = fopen($file, 'w+'); //Open for writing, create if not found
if ($line_number > 0) //Add line at line_number
{
$tmp = array(); //Temporary stack
for ($i = 0; $i < $line_number-1; $i++) //Copy data until the line given
$tmp[] = $lines[$i];
$tmp[] = $text."\n"; //Add line at correct point
for ($i = $line_number; $i < count($lines); $i++) //Copy remaining lines
$tmp[] = $lines[$i];
$lines = $tmp; //Copy the new stack back to the original
}
else $lines[] = "$text\n"; // ...or add line at end
$data = impode("", $lines);
fwrite ($handle, $data);
fclose($handle);
}thank you for helping me!
oh yeah one other thing, i get parse errors if i have lines indented, any way to fix that?
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
i tried this too...
but i get this...
Code: Select all
$content ='I want this written on line 66';
$filepath = '/home/muot/public_html/pages/report.php';
function insert_line($filepath, $content, 66); //If line number is 0 insert at end of file
{
$lines = file($filepath); //Array of lines
$handle = fopen($filepath, 'w+'); //Open for writing, create if not found
if ($line_number > 0) //Add line at line_number
{
$tmp = array(); //Temporary stack
for ($i = 0; $i < $line_number-1; $i++) //Copy data until the line given
$tmp[] = $lines[$i];
$tmp[] = $text."\n"; //Add line at correct point
for ($i = $line_number; $i < count($lines); $i++) //Copy remaining lines
$tmp[] = $lines[$i];
$lines = $tmp; //Copy the new stack back to the original
}
else $lines[] = "$text\n"; // ...or add line at end
$data = impode("", $lines);
fwrite ($handle, $data);
}Parse error: parse error, unexpected '\"', expecting '&' or T_VARIABLE in /home/muot/public_html/pages/muotreport/adding.php on line 70