Fwrite to write data in middle of page?
Posted: Sat Jan 21, 2006 3:43 pm
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!!!
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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);
}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);
}Code: Select all
$file = './foo.txt';
$new_line = 'This line will be inserted';
insert_line($file, $new_line, 68); //Insert at line 68Code: Select all
$content ='I want this written on line 66';
$filepath = '/home/muot/public_html/pages/report.php';Code: Select all
$content ='I want this written on line 66';
$filepath = '/home/muot/public_html/pages/report.php';
insert_line($filepath, $content, 66);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);
}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);
}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