/    Sign up×
Community /Pin to ProfileBookmark

PHP script does not write values to file. Help anyone?

The contents of a simple mailform do not get written to a .txt file. They do get sent to a specified email address, though (you can check it out at [URL=”http://mailbox-it.com/dobbs/tests/writeToFile.html”]Mail Form[/URL]).

[B]Could someone point out the error preventing the PHP code to write values to the file? [/B] The .txt file is in the same directory/folder as the .php file and the .html file. [COLOR=”DarkRed”]Eventually, my goal is to write the mailform contents back to an .html file [B]from this .txt file[/B], into a specified <DIV></DIV> tag. Help![/COLOR]

Here is the code:

[code=php]
<?php

if (isset($_POST[‘submit’])) {
$sent = mail($_POST[‘to’],$_POST[‘subject’],$_POST[‘message’],”From: ” . $_POST[‘from’]);

if ($sent) {
echo “Mail sent”;
}
else{
echo “There was an error sending your mail”;
}

//open write and close file
$fp = fopen(“log.txt”, “a”);
fwrite($fp, $_POST[‘to’] . ” ” . $_POST[‘from’] . “n”);
fclose($fp);
}

?>

[/code]


——————————–
[CODE]

<html>
<head>
<title>Contact Form</title>
</head>
<body>
<form action=”writeToFile.php” method=”post”>
<table>
<tr>
<td>To:</td>
<td><input type=”text” name=”to” /></td>
</tr>
<tr>
<td>From:</td>
<td><input type=”text” name=”from” /></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type=”text” name=”subject” /></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name=”message” rows=”6″ cols=”50″></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type=”submit” name=”submit” value=”send” /></td>
</tr>
</table>
</form>
</body>
</html>

[/CODE]

Please help! I do need it. Thank you.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 02.2007 — Try something like this to see if it helps narrow down the problem:
[code=php]
//open write and close file
$fp = fopen("log.txt", "a");
if($fp !== FALSE)
{
fwrite($fp, $_POST['to'] . " " . $_POST['from'] . "n");
fclose($fp);
}
else
{
if(!is_writeable(getcwd())
{
echo "<p style='color: red'>ERROR: current directory is not writable by this script.</p>n";
}
else
{
echo "<p style='color: red'>ERROR: Unknown error attempting to open file for appending.</p>n";
}
}
[/code]
Copy linkTweet thisAlerts:
@andkhlauthorFeb 03.2007 — I am getting an error: [COLOR="DarkRed"]ERROR: current directory is not writable by this script.[/COLOR]

When I am checking to see if the "log.txt" file is writable -- like this:

[code=php]
if(!is_writable($file))
[/code]


-- then I am getting an error that the file ("log.txt") is NOT writable.

What do I do now? I do not have much experience in all this. I need your assistance.

I appreciate all your help.
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — (1) chmod the directory to make it writeable, or

(2) Find directory that already is.
Copy linkTweet thisAlerts:
@andkhlauthorFeb 03.2007 — [QUOTE=NightShift58](1) chmod the directory to make it writeable, or

(2) Find directory that already is.[/QUOTE]

Thanks for your prompt reply. Would you be willing to explain that in a little more detail as to how to do this "chmod"? Do I have to call my hosting company or can I do it myself? I am sorry, but I plainly do not know what that is as I've never done that before.

Thanks.
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — Usually, with your FTP program (including some FTP-capable editors), you would do this yourself. Look for CHMOD somewhere in there... I think the minimum should be 666 rw- rw- rw-
Copy linkTweet thisAlerts:
@NogDogFeb 03.2007 — The problem is that in most PHP installations on web servers, the scripts are run under a separate user account rather than your personal user account. So you'll probably need to change the permission on the directory where you want to write the file to be read/write/execute for all (octal 777). If you can't find a way to change it via your FTP client, check your web hosting control panel, probably under file system utilities or something like that.
Copy linkTweet thisAlerts:
@andkhlauthorFeb 03.2007 — Thank you very much to all of you, guys! I figured everything out with your help! I had to go my web hosting control panel and change the perimissions there!?
Copy linkTweet thisAlerts:
@andkhlauthorFeb 03.2007 — One more question: Is there away to load the contents of my .txt file right into the <DIV></DIV> tag in my .html page without using PHP? If not, how can it be done with php? Here's step by step of what I've been doing with your help:

1) Send the mailform contents through the PHP script to a .txt file ---[COLOR="DarkRed"] DONE![/COLOR]

2) Send this same info from the .txt file back to an .html page into the <DIV></DIV> tag -- [COLOR="darkred"]in process[/COLOR]

I absolutely have got to go this route. 2 steps.

Thank you, as usually...
Copy linkTweet thisAlerts:
@hastxFeb 03.2007 — just echo the results after the mail form processing...

or if you have to display the exact contents of a file, as is, the only other way I can think of to insert text file contents in a page is with server side includes...but if your going to do that, you may as well stick with php using either file_get_contents() or fread().
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — Since you are fwrite()ing the contents of the mail in append mode, a simple echo will not do.

To display the entire file, use include or require, as your needs may be, if you're not going to do any manipulation of content. It will be the fastest and easiest.

If you must/want to do it with a "real" PHP function, use readfile().

If you have to alter the contents of the file, file_get_contents() or file(), depending on how you want to process the data.

Of course, you would want to put some dressing on that...[code=php]<?php
if (file_exists("log.txt")) {
include "log.txt";
} else {
echo "Sorry, there is currently no log available.";
}
?>[/code]
×

Success!

Help @andkhl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...