Fwrite to write data in middle of page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
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?

Post by nickman013 »

I have a fwrite page, and I need it to write data in a specific part of the page. Is this possible?


Thank You!!!
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: Fwrite to write data in middle of page?

Post by foobar »

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').
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

thanks for responding. is there a tutorial on this somewhere becasue i could not figure that out..

thank you
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It depends. How do you determine the "specific part" of the page?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

i need it to write the data on the 66th line.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You might also want to look at fseek() to move the file pointer whilst you have the file open ;)
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

thank you both for responding, but i cannot figure it out, i looked up all the tutorials, i just need it to write on the 63rd line of the page.


thank you

:D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

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);
}
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

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); 
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$file = './foo.txt';
$new_line = 'This line will be inserted';

insert_line($file, $new_line, 68); //Insert at line 68
Untested....
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

thank you for helping me, but i cant figure this out, i am not that good with php yet, i am still learning.

Code: Select all

$content ='I want this written on line 66';
$filepath = '/home/muot/public_html/pages/report.php';
how would i put that into the code you provided me, i tried myself but i get parse errors. thank you for your time!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$content ='I want this written on line 66';
$filepath = '/home/muot/public_html/pages/report.php'; 

insert_line($filepath, $content, 66);
Noticed a problem with the function... you'll get double line spacing :oops:

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);
}
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

wait, so now i just put

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); 
}
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?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

i tried this too...

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); 
}
but i get this...
Parse error: parse error, unexpected '\"', expecting '&' or T_VARIABLE in /home/muot/public_html/pages/muotreport/adding.php on line 70
Post Reply