PDA

View Full Version : PHP - Need Urgent Help :(



RangeFinder
June 8th, 2002, 19:58
How can I do this in PHP

A text box (web based), where I type information and it gets saved to a .txt file in the server. Again when I go to the text box and type new information, it deletes the content of that .txt file and replaces with new one :)

Is there a script to do this? Or can you please put the code of PHP script which can do this. (I have searched hotscripts but haven’t found anything:()

Thanks in Advance. :)

hohoho
June 9th, 2002, 03:14
$file = fopen("counts.txt", "r+");
$num = fgets($file, 1024);
$num = $num + 1;
rewind($file);
fputs($file, $num);
fclose($file);
This is a part of a file-based counter i made. it opens the counts.txt, where a number is standing. It reads the number, counts it +1, overwrites it and closes the file

bigperm
June 9th, 2002, 03:42
I don't know about PHP, but in perl it would be something like this...



#!/usr/bin/perl

require "cgi-lib.pl";

&ReadParse(*input);

$box_data= $input{'boxdata'};
# Site data. This is the file you are going to be writing to
$sitedata="http://yourdomain.com/yourdatafile.txt"

open(DAT,">$sitedata") || die("Cannot Open File");
print DAT "$boxdata\n";
close(DAT);

print "Content-type: text/html\n\n";
print "<HTML><HEAD>\n";
print "<TITLE>bigperm's file program</TITLE>\n";
print "</HEAD>\n";
print "<BODY><p>Your data was entered successfully</p>\n";
print "</BODY></HTML>";


I am sure in PHP it's similar.

RangeFinder
June 9th, 2002, 07:04
mitja,
I do not need a counter :(

bigperm,
I need it in PHP, Thanks anyway :o

This is what I exactly need:
A text box (web based), where I type information and it gets saved to a .txt file in the server. Again when I go to the text box and type new information, it deletes the content of that .txt file and replaces with new one.

hohoho
June 9th, 2002, 07:33
i know you don't ned it, but it's an example how to do it

bigperm
June 9th, 2002, 07:55
Well, I hunkered down and figured it out. You can thank me later.

Step 1: Make an empty data file, upload it, and chmod it to 666. It should be in the same folder as write.php
Step 2: Configure Bigperm's File Writing to Magic Program... it's easy....

Step 3:Save this as write.php and upload it.

<?php
//This is the name of the file you are writing to. Make sure it's chmod 666
$data_file="data.dat";
//Do not edit below here unless you know what you are doing

if ($submit_data) {
$file_name = $data_file;
$file_pointer = fopen($file_name, "w");
$lock = flock($file_pointer, LOCK_EX);
if ($lock) {
fwrite($file_pointer, $submit_data);
flock($file_pointer, LOCK_UN);
}
fclose($file_pointer);
print "data written to file successfuly\n";
print "You entered $submit_data and it was added to the file $data_file\n";
}
?>
<html>
<head>
<title>Bigperm's File Writing to Magic Program</title>
</head>
<body>
<form action="<? $PHP_SELF ?>">
<p><input type="text" size="20" name="submit_data"><input
type="submit" name="submit" value="Submit"></p>
</form>
</body>
</html>

There you go!

RangeFinder
June 9th, 2002, 08:21
Thank U very much, bigperm :)

RangeFinder
June 9th, 2002, 08:54
It works for text :) But does not work for HTML :( Can you please fix it :confused:

I keeps adding \ every where :o

Thanks in Advance

bigperm
June 10th, 2002, 00:39
OK... here it is. v.2



<?php
//This is the name of the file you are writing to. Make sure it's chmod 666
$data_file="data.dat";
//Do not edit below here unless you know what you are doing
if ($html_checked) {
$submit_data = stripslashes($submit_data);
}
if ($submit_data) {
$file_name = $data_file;
$file_pointer = fopen($file_name, "w");
$lock = flock($file_pointer, LOCK_EX);
if ($lock) {
fwrite($file_pointer, $submit_data);
flock($file_pointer, LOCK_UN);
}
fclose($file_pointer);
print "data written to file successfuly\n";
print "You entered $submit_data and it was added to the file $data_file\n";
}
?>
<html>
<head>
<title>Bigperm's File Writing to Magic Program</title>
</head>
<body>
<form action="<? $PHP_SELF ?>">
<p><input type="text" size="20" name="submit_data">
<input type="checkbox" name="html_checked">Is this html?
<input type="submit" name="submit" value="Submit"></p>
</form>
</body>
</html>-

RangeFinder
June 10th, 2002, 09:41
It Works! Thank you very much :)