Need Help Saving Text Box to Text File
I cant seem to get this to work ... I am trying to save text box info to text file using PHP ...
<?php
$About = @$_POST["About_Box"];
// Set the string to be written to the file
$values = "$About \r\n";
// Open the file for truncated writing
$fp = @fopen("../textFiles/careersE.txt", "w") or die("Couldn't open careersE.txt for writing!");
$numBytes = @fwrite($fp, $values) or die("Couldn't write values to file!");
@fclose($fp);
echo "Wrote $numBytes bytes to careersE.txt successfully!";
?>
Any help is welcomed
Mushimonster
Suzanne posted this at 17:54 — 23rd March 2004.
She has: 5,507 posts
Joined: Feb 2000
What errors are you getting? Also, please provide an url so we can look at the problem more closely, ot at the very least provide the rest of the code.
Greg K posted this at 18:15 — 23rd March 2004.
He has: 2,145 posts
Joined: Nov 2003
What are the result so far? Is it giving an error? Is it actually creating the file (without the content) or not? What permissions are on the file and/or the directory that the file is trying to write to?
Did you try it with all the @'s removed to see any possible warnings?
I just now did the following:
Placed your code into a file called test.php and made a file called test.html that had the following:
<html>
<head><title>Test File</title></head>
<body>
<form name="form1" method="post" action="test.php">
<textarea name="About_Box" id="About_Box"></textarea>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
Then in the directory above where these files were at, I created a directory called textFiles and set it to be written by everyone (so the web server could write to it).
I ran the test page, and it worked fine, told me how many bytes it wrote, and the file was there with the content.
My guess is file permissions on the directory.
-Greg
mushimonster posted this at 07:31 — 25th March 2004.
They have: 4 posts
Joined: Mar 2004
Errors ... No errors actually the code tells me it is wrote x number of bytes but when i open the text file it is blank.
Permissions ... I didnt think about that. See I am using PHP triad for windows. I wonder if this is the problem. How can I find out how to modify permissions with Windows PHP.
No URL yet ... I am building on my laptop before I deploy it ...
The Full Code ...
CODE FOR "editTextTest.php"
TestForm
TO EDIT THE **** PAGE USE THE FOLLOWING FORM
ORIGINAL TEXT:
<?php
$fp = fopen ("../textFiles/test.txt", "r");
while (!feof ($fp)) {
$content = fgets( $fp, 4096 );
echo $content;
}
fclose ($fp);
?>
=============================================
ENTER REVISED TEXT HERE:
>">
CODE FOR "saveTextTest.php"
<?php
$TxtBox = $_POST ["BigBox"];
$values = "$TxtBox";
$file_name = "..\textFiles\test.txt";
$fp = fopen($file_name, "w");
fwrite($fp, "$TxtBox");
fclose($fp);
echo "Your Page has been updated /n";
?>
As you can see I modified this a little trying to see if I can make it work but the code does not like me. Thank you for looking at this. So you really think it is permissions ? Let me try some thinking then...
- Adam
Greg K posted this at 18:27 — 25th March 2004.
He has: 2,145 posts
Joined: Nov 2003
I'm not sure about permissions with windows. One thing I did want to point out is to make sure you use functions to strip out/convert html code from the $content variable before displaying it.
-Greg
kb posted this at 01:16 — 26th March 2004.
He has: 1,380 posts
Joined: Feb 2002
for future reference...preface all PHP code with [php] and end with /php in brackets, html is [html], and other code is [code], whereas quotes are
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.