Re: Failed to write to a text file (text file is RW) [message #171423 is a reply to message #171421] |
Sun, 02 January 2011 23:54 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
justaguy wrote:
> Denis McMahon wrote:
>> On 02/01/11 20:19, justaguy wrote:
>>> I dig up the following script. Added a simple HTML file with a form
>>> field named "vote" and have it send to the following php script.
>>> However, it failed to write to the poll_result.txt file. Why?
>>
>> The following seems to work for me.
>>
>> <?php
>> $filename = "poll_result.txt";
>> if (isset($_POST['vote'])) {
>> $vote = intval($_POST['vote']); // get this vote
>> $content = file($filename); // read result file
>> $array = explode("||", $content[0]);
>> $yes = intval($array[0]); // get stored results
>> $no = intval($array[1]);
>> if ($vote == 1) $yes++; // increment one
>> if ($vote == 0) $no++;
>> $insertvote = $yes."||".$no; // new result string
>> }
>> else {
>> $insertvote = "0||0\n"; // reset result string
>> $yes = 0;
>> $no = 0;
>> }
>> $fp = fopen($filename,"w"); // store results
>> fputs($fp,$insertvote);
>> fclose($fp);
>> echo <<<EOT
>> […]
>> EOT;
>> ?>
>
> I still failed to write to the poll_result.txt file and I noticed
> there's a slight difference between this file's access attributes
> compared to a similar target text file with a similar script and
> process. The other target text file has rwxrwxrwx attributes while
> this one has rwrwrw attributes.
This is commonly written `rw-rw-rw-' or `chmod 666' instead. Rule of thumb:
r = 4, w = 2, x = 1; addition provides the number.
> I thought the global writable would suffice but it seems I was incorrect.
> My ftp client failed to do chmod 755 to the global x (execute) attribute
> to the file.
You would be well-advised to get the basics right first.
1. If you had previously `rw-rw-rw-' or `chmod 666', you would need
to do `chmod o+x' or `chmod 777' to get `rwxrwxrwx'.
2. In which mode you can open a file has not so much to do with that
file's permissions. More important are the ownership of the file,
the permissions of the directory the file is located in, and who/
what else has that file already open.
3. A text file's *executable* bit of course has nothing to do with
whether or not you can read from or write to this file (regarding
PHP, it is relevant for CGI scripts only). However, the read and
executable permission bits of its directory are relevant for reading,
and the directorie's write permission bit is relevant for creating
a new file.
4. There is no "global writable" or "global x (execute) attribute".
The first _permissions_ triple applies only to the owner of the file,
the second to all users of the group which owns the file, and the
the third one to all *other* users.
Usually PHP is not run by the owner or the group of the file, but
by the Web server; this is either run by root or by a special system
user (e.g. www-data), which usually is not the owner nor is its group
the owning group.
So the *third* permissions triple is relevant, not the first.
And please trim your quotes to the parts you are referring to.
PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
|
|
|