PDA

Click to See Complete Forum and Search --> : question


Pouncer
Sep 15th, 2005, 07:14 PM
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)

{yak}
Sep 15th, 2005, 07:21 PM
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

Pouncer
Sep 15th, 2005, 07:33 PM
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?

{yak}
Sep 15th, 2005, 07:40 PM
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?

Pouncer
Sep 15th, 2005, 07:44 PM
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.

k1ll3rdr4g0n
Sep 15th, 2005, 07:45 PM
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?

Pouncer
Sep 15th, 2005, 07:48 PM
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 :ehh:

{yak}
Sep 15th, 2005, 08:08 PM
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 :ehh:


$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

Pouncer
Sep 15th, 2005, 08:11 PM
thank you, that helps me, ill work on it now and post my results back!!

Pouncer
Sep 15th, 2005, 08:16 PM
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
$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!

ninjanutz
Sep 15th, 2005, 08:44 PM
u need a semicolon here... $somecontent = $_GET['txt'] so it should be... $somecontent = $_GET['txt'] ;

Pouncer
Sep 16th, 2005, 03:49 AM
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?

{yak}
Sep 16th, 2005, 06:59 AM
Add the newline character to the end of your string:

$somecontent = $_GET['txt'] . "\n";


Good luck

Pouncer
Sep 16th, 2005, 07:16 AM
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"



<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

{yak}
Sep 16th, 2005, 07:31 AM
Did you add the "\n" onto the string? :confused:

Pouncer
Sep 16th, 2005, 08:01 AM
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

{yak}
Sep 16th, 2005, 08:07 AM
So it's html? I'm kinda lost, but if it's html, then, add a break tag:
<br>

Pouncer
Sep 16th, 2005, 09:25 AM
yep works, thanks alot! ;)