Page 1 of 1

Fwrite to write data in middle of page?

Posted: Sat Jan 21, 2006 3:43 pm
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!!!

Re: Fwrite to write data in middle of page?

Posted: Sat Jan 21, 2006 3:54 pm
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').

Posted: Sat Jan 21, 2006 5:48 pm
by nickman013
thanks for responding. is there a tutorial on this somewhere becasue i could not figure that out..

thank you

Posted: Sat Jan 21, 2006 7:08 pm
by Ambush Commander
It depends. How do you determine the "specific part" of the page?

Posted: Sat Jan 21, 2006 10:55 pm
by nickman013
i need it to write the data on the 66th line.

Posted: Sat Jan 21, 2006 11:31 pm
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

Posted: Sun Jan 22, 2006 7:50 am
by Chris Corbyn
You might also want to look at fseek() to move the file pointer whilst you have the file open ;)

Posted: Sun Jan 22, 2006 8:18 am
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

Posted: Sun Jan 22, 2006 9:00 am
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);
}

Posted: Sun Jan 22, 2006 10:26 am
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); 
}

Posted: Sun Jan 22, 2006 10:30 am
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....

Posted: Sun Jan 22, 2006 10:46 am
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!

Posted: Sun Jan 22, 2006 11:03 am
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);
}

Posted: Sun Jan 22, 2006 11:06 am
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?

Posted: Sun Jan 22, 2006 11:35 am
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