|
-
Sep 15th, 2005, 07:14 PM
#1
Thread Starter
Frenzied Member
question
do you guys know how i can write stuff to a text file in my php area?
for e.g. to currently view my php stuff through webpage this is what i do:
http://localhost/<dir>/filename
or
http://<my-ip>/<dir>/filename
(e.g.: http://localhost/login.html)
i want to know how i can write to a file called Pyscho.txt, and then view this file on the web by, for.e.g
http://localhost/Pyscho.txt
any help would be greatly appreciated
ps: my pages and files are in the 'www' directory (this is where all php files are kept i think)
-
Sep 15th, 2005, 07:21 PM
#2
Lively Member
Re: question
Hi,
Look at http://us2.php.net/fopen, also, check out the ' See also' links half way down the page, it will have information on how to write to the file.
Good luck
-
Sep 15th, 2005, 07:33 PM
#3
Thread Starter
Frenzied Member
Re: question
thanks ill read that but what im trying to do is someting like:
http://localhost/fwrite.php<something-here> (navigate from the web - not sure aboutt he syntax of the link? can someone give the correct one?)
and in the file fwrite.php i have to have a function for writing text to test.txt?
-
Sep 15th, 2005, 07:40 PM
#4
Lively Member
Re: question
I'm not sure I understand? Create a page called fwrite.php, and put in your code to write to whatever text file. Then to run the page, you go to: http://localhost/fwrite.php
Or did you mean like, http://localhost/fwrite.php?file=filenamehere
Or am I misunderstanding you completley?
-
Sep 15th, 2005, 07:44 PM
#5
Thread Starter
Frenzied Member
Re: question
well a freind of mine had something like:
www.his-site.org/fwrite.php?entry=blah
and it used to write the word 'blah' to the text file called 'test.txt'
which could be viewed by:
www.his-site.org/test.txt
i hope you understand me now, thanks for your replies and help.
-
Sep 15th, 2005, 07:45 PM
#6
Fanatic Member
Re: question
Oh, I think I see what hes doing..
Did you put a textbox on your page and a submit button and want it to write out to a file?
-
Sep 15th, 2005, 07:48 PM
#7
Thread Starter
Frenzied Member
Re: question
yes thats bang on!!
i got a text box there and when i click the button i want it to write the contents of this text box to the file
(by the way, this textbox and button is on a vb form so im using the webBrowser component to do something like:
web.Navigate "http://localhost/fwrite.php?..whateverhere )
but what i need is how to construct the link and the fwrite.php file
Last edited by Pouncer; Sep 15th, 2005 at 07:53 PM.
-
Sep 15th, 2005, 08:08 PM
#8
Lively Member
Re: question
Ok that helps. You're going to use $_GET['name of parameter'] to retreive the value. and then write that to the file, you can look at the link I gave you to write the file but this is an example, kinda
PHP Code:
$value = $_GET['txt']);
if(!isset($value))
{
// theres no value...redirect or something
}
// now do your insert code, of course you validate $value to make sure everything is what you want
// when you write to your txt file, write $value.
// your link would look like http://localhost/fwrite.php?txt=and then the value of your vb text box or whatever
-
Sep 15th, 2005, 08:11 PM
#9
Thread Starter
Frenzied Member
Re: question
thank you, that helps me, ill work on it now and post my results back!!
-
Sep 15th, 2005, 08:16 PM
#10
Thread Starter
Frenzied Member
Re: question
im getting a strange error:
im using this:
wb.Navigate "http://localhost/fwrite.php?txt=testline"
and getting an error:
Parse error: syntax error, unexpected T_IF in c:\wamp\www\fwrite.php on line 6
This is the code from the fwrite.php file:
PHP Code:
<?php
$filename = 'test.txt';
$somecontent = $_GET['txt']
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
do you guys see anything wrong? im trying to get the word 'testline' to write to the file!
-
Sep 15th, 2005, 08:44 PM
#11
Hyperactive Member
Re: question
u need a semicolon here... $somecontent = $_GET['txt'] so it should be... $somecontent = $_GET['txt'] ;
-
Sep 16th, 2005, 03:49 AM
#12
Thread Starter
Frenzied Member
Re: question
ok thanks it works beautifully. but 1 problem..
when i do it twice, for e.g..
wb.Navigate "http://localinfo/fwrite.php?txt=text1"
wb.Navigate "http://localinfo/fwrite.php?txt=text1"
it writes it to the textfile but its like this:
text1text2
isnt it possible to get it like
text1
text2
..in the text file?
-
Sep 16th, 2005, 06:59 AM
#13
Lively Member
Re: question
Add the newline character to the end of your string:
PHP Code:
$somecontent = $_GET['txt'] . "\n";
Good luck
-
Sep 16th, 2005, 07:16 AM
#14
Thread Starter
Frenzied Member
Re: question
thanks for the reply but my text file is like this after i write to the file
wb.Navigate "http://localinfo/fwrite.php?txt=text1"
wb.Navigate "http://localinfo/fwrite.php?txt=text2"
wb.Navigate "http://localinfo/fwrite.php?txt=text3"
PHP Code:
<html>
<h1>Header</h1>
text1<newlinecharhere>text2<newlinecharhere>text3
</html>
and when i view it in explorer:
http://localhost/test.txt
it shows the header fine, but the words are stil like this:
text1text2text3
-
Sep 16th, 2005, 07:31 AM
#15
Lively Member
Re: question
Did you add the "\n" onto the string?
-
Sep 16th, 2005, 08:01 AM
#16
Thread Starter
Frenzied Member
Re: question
yep:
$somecontent = $_GET['txt'] . "\n";
it works without the html tag stuff, but i want to mae it work *with* the html tags so i can have a nice heading at the top of the text file when viewed through i.e
-
Sep 16th, 2005, 08:07 AM
#17
Lively Member
Re: question
So it's html? I'm kinda lost, but if it's html, then, add a break tag:
<br>
-
Sep 16th, 2005, 09:25 AM
#18
Thread Starter
Frenzied Member
Re: question
yep works, thanks alot!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|