How do I automatically copy text generated from a form into another file in PHP?
So on my site, I have a form set up, and when one enters information, it spits back another page with the results all neatly formatted in about 10 lines. How can I make PHP copy those 10 lines and append it to the end of another file on my site? If this is only possible in JavaScript, could you please tell me so, so that I may post in the Javascript forum?
Let me provide a link to my website to illustrate: please visit new/entry.hostei.com (DELETE THE /), and click "Submit Query" at the bottom. You need not type anything in the boxes. Viewing the page source, the lines I would want copied are the line starting after < /head>, through the next ten lines (until the blank space starts).
Note: I do not want to replace the "destination file," but merely add lines of code onto the end.
I've tried to search for this on Google, but it involves too many keywords and so there is not much useful output. I've 开发者_Go百科also asked on another forum, but so far they have not been able to provide any useful output.
How can I make PHP copy those 10 lines and append it to the end of another file on my site?
Easily. Given $data
is an array of the lines to add, and $filename
is the name of the file being appended to:
// Open the file in Append mode, with the file pointer placed at the end of the file.
// The file will be created if it does not exist.
$fh = fopen($filename, 'a+');
// Establish a lock on the file.
flock($fh, LOCK_EX);
// Write each line in the array and a newline.
foreach($data as $line) {
fwrite($fh, $line);
fwrite($fh, "\n");
}
// Expressly release the lock and close the file.
flock($fh, LOCK_UN);
fclose($fh);
If your $data
is a string instead of an array,
// Open the file in Append mode, with the file pointer placed at the end of the file.
// The file will be created if it does not exist.
$fh = fopen($filename, 'a+');
// Establish a lock on the file.
flock($fh, LOCK_EX);
// Write the data and a newline.
fwrite($fh, $data);
fwrite($fh, "\n");
// Expressly release the lock and close the file.
flock($fh, LOCK_UN);
fclose($fh);
Also,
If this is only possible in JavaScript, could you please tell me so
Quite the opposite, Javascript does not have access to the filesystems of either your server or your client's system.
You might want to try file_put_contents:
file_put_contents( "filename.txt", $line, FILE_APPEND );
This would be placed inside of your PHP file that outputs the results to the screen, and you will need to replace "$line" with the variable that holds the line that is being output. Change "filename.txt" to whatever the text file you want the lines to be added to. Any valid filename will work.
Depending on how your code is written, there may be other things involved. You could keep it very simple and do a separate file_put_contents for each line you are dealing with:
file_put_contents( "filename.txt", $line1, FILE_APPEND );
file_put_contents( "filename.txt", $line2, FILE_APPEND );
file_put_contents( "filename.txt", $line3, FILE_APPEND );
...etc...
Where the above, $line1, $line2, $line3 is replaced by the variable name of the output on your result page.
If the line outputs are in a loop, you will probably just need file_put_contents once as in my first example at the top. Again, it all depends on some of the details of your existing code.
Another thing to look out for, you may need to add newlines (\n) at the end of the lines you write to the disk. This may be needed to keep your lines from appearing all-together on the same line in your output file:
file_put_contents( "filename.txt", $line . "\n", FILE_APPEND );
See here for more info on file_put_contents: http://www.php.net/file_put_contents
精彩评论