2019-07-06 14:46:28

Hi.
So, I'm trying to make a thing that posts to a php script that beta testers can use to leave comments. The client seems to work just fine, but for some reason php isn't writing the comment to the comments.txt file like it should be. Here's the code.
<?php
$comment=$_POST["comment"];
if($comment=="")
{
die("That comment is invalid.");
}
file_put_contents("comments.txt",$comment);
echo "Comment successfully posted. Your comment: $comment";
?>

Use the below link to get about 500 or so mb of extra dropbox space when you create an account.
https://db.tt/dDngEzFLo7

2019-07-06 15:08:35

well, when you are writing to file you would do
fwrite(myfile.txt);
$txt = "ilya loves cats";
also make sure php has permission to access that file.

Have a lovely day.

2019-07-06 15:14:27

You have to tell it to write to the file.

01101001 01110100 01110100 01100101 01101110 00100000
--------------------------
This is the ultra nerdieness. My signature is written in binary.

Thumbs up

2019-07-06 15:21:09

Uh, but isn't that what file put contents does anyway?

Use the below link to get about 500 or so mb of extra dropbox space when you create an account.
https://db.tt/dDngEzFLo7

2019-07-06 15:44:51

Make sure file permissions are properly set. If you're doing this on a Linux server, try:

sudo chmod 753 your_script.php

If you wish, use the -v optional argument to make sure it set it properly.

2019-07-06 15:46:51 (edited by Ilya 2019-07-06 15:47:33)

file put  and fopen, are both functions that all most do the same thing.
however with fopen you have a little more control.

Have a lovely day.

2019-07-06 16:06:14

Currently it is set to 777. Is this ok?

Use the below link to get about 500 or so mb of extra dropbox space when you create an account.
https://db.tt/dDngEzFLo7

2019-07-06 16:35:39

better to make it 644,  because the owner has to be able to edit it, everyone needs to be able to read it, and nobody needs to execute it (in the strictly unix sense. php is the executable and it's reading the files, parsing them, and doing stuff with them, so unix isn't actually executing them.

Have a lovely day.

2019-07-06 16:41:46 (edited by leibylucw 2019-07-06 16:43:38)

It's good practice to [not] use 777. This means that the file owner, other members of the group the owner belongs to, and all other users can read, write, and execute the file. In short, any and all users has unrestricted access to the file.

A quick summary of what these numbers mean:

The three numbers correlate to the different roles I mentioned. Owner, group, other users. There are two ways of setting file permissions. Using the three numbers is called octal notation. It uses binary to set the appropriate permissions for each role.

4 = read
2 = write
1 = execute

Add any of these to grant the permissions you wish. For example:

7 = 4+2+1, read, write, and execute (should be used for the owner)
6 = 4+2, read and write
5 = 4+1, read and execute

and so on and so forth. 0 means there is no access granted on any level to the role you've set.

Directories work differently, and this can also play a role in why your file might not be written to the directory. Ensure that the directory itself, not the files inside the directory, is set to allow being written to.

For more help, consult
this page.
and this
guide from Linode

Edit: @8 is right, you don't need execute, and it's also a good practice to disable settings that you know won't be used. It's not hurting you to have it 755, but it does make more sense to have 644, just depends on your view.

2019-07-06 17:31:55

Hi,
In addition to what Number 9 said, I see your overwriting your "comments.txt" file.
Make sure to set "FILE_APPEND" flag to append your comments to the comments.txt file.
Otherwise, Your "comments.txt" will be overwritten everytime anyone posts something.
Not related to your issue, Just something i noticed.

Be  fair minded, and let people to think in the way they want to.
People don't see the world in the way you do!

2019-07-06 17:48:36

Ok so possibly stupid question, but what is the append flag in php? I know that for instance it is ab in bgt, but this could be different in php.

Use the below link to get about 500 or so mb of extra dropbox space when you create an account.
https://db.tt/dDngEzFLo7

2019-07-06 19:03:30 (edited by Ilya 2019-07-06 19:04:24)

ok, so in php lets say you wanted to append  a string to a diffrent string you would do this
1. <?php
2. a = "Hello";
3. a .= " World!";
4. echo a; // Outputs: Hello World!
5. ?>
now, if you wanted to append  info to a file do this
my_file = 'file.txt';
handle = fopen(my_file, 'a') or die('Cannot open file:  '.$my_file);
data = 'New data line 1';
fwrite($handle, data);
new_data = "\n".'New data line 2';
fwrite(handle, new_data);

and lets say, you closed that comment topick.
, you could do that by deleting the file like this.
my_file = 'file.txt';
unlink(my_file);

Have a lovely day.

2019-07-09 23:31:02

Odly enough, I seem to have the same problem writing to a file. The file in question doesn't exist yet, but fopen should create it anyway. Here's the code that isn't working.
<?php
$myfile = fopen("date.txt", "w")
$txt = " . date("l-m-d-Y") . \n";
fwrite($myfile, $txt);
fclose($myfile);

?>
Any idea what I'm not doing right here?

That Guy. Serving those people since that time. To contact, use that info.

2019-07-10 12:34:35 (edited by leibylucw 2019-07-10 12:34:57)

@13, see posts 5 and 9.

2019-07-10 13:08:44

I actuaLly figured it out, and it didn't have anything to do with my code. For some reason, the comments.txt file I'd created didn't have write permitions enabled, so the php script couldn't write to it. If using win scp, hit alt + enter on the txt file to edit, then make sure all the w checkboxes are checked.

Use the below link to get about 500 or so mb of extra dropbox space when you create an account.
https://db.tt/dDngEzFLo7

2019-07-12 00:00:39

Remember, the directory that these files reside in needs to also have the appropriate permissions set. It isn't just about the files themselves.

2019-07-12 15:13:07

Hi!
In the long rung you'd be better off using a database rather than dealing with files yourselves.
Cheers Perry

Thumbs up

2019-07-12 20:06:32

yep, it would be better to set up a my sql data base.

Have a lovely day.

2019-07-12 21:05:32 (edited by Reza.jdp 2019-07-12 21:07:05)

@13:
I noticed you forgot to put a semicolon (;) at the end of the first line.
Make sure you have no syntax error.

Be  fair minded, and let people to think in the way they want to.
People don't see the world in the way you do!