Click to See Complete Forum and Search --> : Post , add, copy to html file


Peuplarchie
05-22-2007, 09:00 PM
I'm posting data trough a form and adding it to an html file with the following :


<?php
echo "copiage de fichier debut";
// your new data + newline
$new_line = stripslashes($_POST['codesource'])."\n\r";
// the filepath
$file = 'upload/aaatest.html';
// the old data as array
$old_lines = file($file);
// add new line to beginning of array
array_unshift($old_lines,$new_line);
// make string out of array
$new_content = join('',$old_lines);
$fp = fopen($file,'w');
// write string to file
$write = fwrite($fp, $new_content);
fclose($fp);
echo "copiage de fichier Fin";
echo "Redirection.....";
?>



this code add "codesource" to "upload/aaatest.html"

It add it to the bigginig of the file, How can I choose the line "codesource" is added within the file , ex : count 100 lines and start adding text after 100 lines ????


I also need this code to add a log part,
It would add to the source code of the html file, in comment, the time&date, the ip, the name, email.

I know how to show the time or ip of a user on screen but i don't want it to be seen, only in the sourcecode of the html, as logs ????

hastx
05-23-2007, 09:20 AM
One way is to create a temp variable to append. Start a counter to find which line to insert to.


<?
$file="your_text_file.txt";//define the file to open
$lines=file($file);//load the lines to an array
$insert_line = 3;//the line to insert at...could be a post variable
$insert_text = "this text has been inserted by PHP"."\r\n";//this could be a post variable, but at any case make sure to append the var with the new line, otherwise it will run together
$temp="";//start a temp variable to work with
$count=1;//initialize a counter


foreach($lines as $line){
($count != $insert_line) ? $temp .= $line : $temp .= $insert_text . $line;
$count++;
}

//Im printing the results, but you would open the the file in write mode and write the contents of temp to the file.

echo"$temp";
?>