| | |||||
| ||||||||
| CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| Senior Member Join Date: Aug 2002
Posts: 1,255
![]() ![]() | Opening a text file and displaying the results in browser with PHP, Perl, and Python Here's a few code snippets of the various methods different languages use for manipulating text files. Each example opens file.txt for appending, writes a string to it, reopens the file for reading and outputs the entire content of the text file to the browser. If you want to overwrite the content of the file rather than append to it you can switch the "a" to "w" for both PHP and Python and change the ">>" to ">" for Perl. There's other methods of doing this for each language, but this should give a good idea of how certain functions for each language compare to each other. PHP Code: <?
$file = fopen ("file.txt", "a");
fwrite ($file, "This is a PHP test<br />n");
fclose ($file);
$file = fopen ("file.txt", "r");
$lines = fread ($file, filesize ("file.txt"));
fclose ($file);
echo $lines;
?> ????: NamePros.com http://www.namepros.com/code/16010-opening-text-file-displaying-results-browser.html Code: #!/usr/bin/python
print "Content-type: text/html\n\n"
file = open('file.txt','a')
file.write("This is a Python test<br />n")
file.close()
file = open('file.txt','r')
for lines in file.readlines():
print lines,
file.close() Code: #!/usr/bin/perl print "Content-type: text/html\n\n"; open (FILE, ">>file.txt"); print FILE "This is a Perl test<br />n"; close (FILE); open (FILE, "file.txt"); @lines = <FILE>; close (FILE); print @lines; |
| |