FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Failed to write to a text file (text file is RW)
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Failed to write to a text file (text file is RW) [message #171414] Sun, 02 January 2011 20:19 Go to next message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma: 0
Junior Member
Hi,

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?

Thanks as usual.

<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 1)
{
$yes = $yes + 1;
}
if ($vote == 0)
{
$no = $no + 1;
}


// debug
echo $yes;
echo $no;

// output and comments
/*
Seed poll_result.txt to read 0||0

It's always 10 even though I checked the YES option several times
obviously it failed to write to the poll_result.txt file.

Checked the poll_result.txt, it's 0||0

*/


//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
Re: Failed to write to a text file (text file is RW) [message #171415 is a reply to message #171414] Sun, 02 January 2011 20:35 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/2/2011 3:19 PM, justaguy wrote:
> Hi,
>
> 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?
>
> Thanks as usual.
>
> <?php
> $vote = $_REQUEST['vote'];
>
> //get content of textfile
> $filename = "poll_result.txt";
> $content = file($filename);
>
> //put content in array
> $array = explode("||", $content[0]);
> $yes = $array[0];
> $no = $array[1];
>
> if ($vote == 1)
> {
> $yes = $yes + 1;
> }
> if ($vote == 0)
> {
> $no = $no + 1;
> }
>
>
> // debug
> echo $yes;
> echo $no;
>
> // output and comments
> /*
> Seed poll_result.txt to read 0||0
>
> It's always 10 even though I checked the YES option several times
> obviously it failed to write to the poll_result.txt file.
>
> Checked the poll_result.txt, it's 0||0
>
> */
>
>
> //insert votes to txt file
> $insertvote = $yes."||".$no;
> $fp = fopen($filename,"w");
> fputs($fp,$insertvote);
> fclose($fp);
> ?>

Are you sure you're looking at the same file you're writing to? You're
using a relative path, after all (which would place the data file in
your web path, where it could be downloaded by anyone - not a good idea).

Does the user (if this is running under the web server, the web server
user) have permission to write to the file?

And finally, this is totally unsafe. There is nothing stopping two
different people from opening the file at the same time, potentially
corrupting your date.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171416 is a reply to message #171415] Sun, 02 January 2011 21:07 Go to previous messageGo to next message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma: 0
Junior Member
On Jan 2, 3:35 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 3:19 PM, justaguy wrote:
>
>
>
>> Hi,
>
>> 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?
>
>> Thanks as usual.
>
>> <?php
>> $vote = $_REQUEST['vote'];
>
>> //get content of textfile
>> $filename = "poll_result.txt";
>> $content = file($filename);
>
>> //put content in array
>> $array = explode("||", $content[0]);
>> $yes = $array[0];
>> $no = $array[1];
>
>> if ($vote == 1)
>>    {
>>    $yes = $yes + 1;
>>    }
>> if ($vote == 0)
>>    {
>>    $no = $no + 1;
>>    }
>
>> // debug
>> echo $yes;
>> echo $no;
>
>> // output and comments
>> /*
>> Seed poll_result.txt to read  0||0
>
>> It's always 10 even though I checked the YES option several times
>> obviously it failed to write to the poll_result.txt file.
>
>> Checked the poll_result.txt, it's 0||0
>
>> */
>
>> //insert votes to txt file
>> $insertvote = $yes."||".$no;
>> $fp = fopen($filename,"w");
>> fputs($fp,$insertvote);
>> fclose($fp);
>> ?>
>
> Are you sure you're looking at the same file you're writing to?  You're
> using a relative path, after all (which would place the data file in
> your web path, where it could be downloaded by anyone - not a good idea).
>
> Does the user (if this is running under the web server, the web server
> user) have permission to write to the file?
>
> And finally, this is totally unsafe.  There is nothing stopping two
> different people from opening the file at the same time, potentially
> corrupting your date.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Well, on "where it could be downloaded by anyone", how come? The
processPoll.php file or a similar process php script is a server
script,
average user can't mess up with it.

On file permission, sorry, I forgot to mention, it's Read and Write.
A similar php script executes fine.

On your last point of " this is totally unsafe. There is nothing
stopping two
> different people from opening the file at the same time, potentially
> corrupting your date."
Once we fix the above two problems what could we do about it? I'm
totally new to php btw.

Many thanks.
Re: Failed to write to a text file (text file is RW) [message #171418 is a reply to message #171414] Sun, 02 January 2011 22:00 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Yes-No Poll</title>
</head>
<body style="padding:1em 3em">
<form method="post" action="quickpoll.php" name="f1" id="f1">
<p>Yes: <input type="radio" name="vote" id="v1_1" value="1"><br>
No: <input type="radio" name="vote" id="v1_0" value="0"><br>
<input type="submit" value="Vote Now" name="s1" id="s1"></p>
</form>
<p>The results so far:<br>
Yes: $yes votes<br>
No: $no votes</p>
</body>
</html>
EOT;
?>

Rgds

Denis McMahon
Re: Failed to write to a text file (text file is RW) [message #171419 is a reply to message #171416] Sun, 02 January 2011 22:13 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/2/2011 4:07 PM, justaguy wrote:
> On Jan 2, 3:35 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>> On 1/2/2011 3:19 PM, justaguy wrote:
>>
>>
>>
>>> Hi,
>>
>>> 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?
>>
>>> Thanks as usual.
>>
>>> <?php
>>> $vote = $_REQUEST['vote'];
>>
>>> //get content of textfile
>>> $filename = "poll_result.txt";
>>> $content = file($filename);
>>
>>> //put content in array
>>> $array = explode("||", $content[0]);
>>> $yes = $array[0];
>>> $no = $array[1];
>>
>>> if ($vote == 1)
>>> {
>>> $yes = $yes + 1;
>>> }
>>> if ($vote == 0)
>>> {
>>> $no = $no + 1;
>>> }
>>
>>> // debug
>>> echo $yes;
>>> echo $no;
>>
>>> // output and comments
>>> /*
>>> Seed poll_result.txt to read 0||0
>>
>>> It's always 10 even though I checked the YES option several times
>>> obviously it failed to write to the poll_result.txt file.
>>
>>> Checked the poll_result.txt, it's 0||0
>>
>>> */
>>
>>> //insert votes to txt file
>>> $insertvote = $yes."||".$no;
>>> $fp = fopen($filename,"w");
>>> fputs($fp,$insertvote);
>>> fclose($fp);
>>> ?>
>>
>> Are you sure you're looking at the same file you're writing to? You're
>> using a relative path, after all (which would place the data file in
>> your web path, where it could be downloaded by anyone - not a good idea).
>>
>> Does the user (if this is running under the web server, the web server
>> user) have permission to write to the file?
>>
>> And finally, this is totally unsafe. There is nothing stopping two
>> different people from opening the file at the same time, potentially
>> corrupting your date.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Well, on "where it could be downloaded by anyone", how come? The
> processPoll.php file or a similar process php script is a server
> script,
> average user can't mess up with it.
>

That's fine - but the data file itself is in the same directory - where
a knowledgeable user has direct access to it.

> On file permission, sorry, I forgot to mention, it's Read and Write.
> A similar php script executes fine.
>

read/write for whom?

> On your last point of " this is totally unsafe. There is nothing
> stopping two
>> different people from opening the file at the same time, potentially
>> corrupting your date."
> Once we fix the above two problems what could we do about it? I'm
> totally new to php btw.
>
> Many thanks.
>
>

Has nothing to do with PHP - this can occur in ANY language. You need
to learn all about how to work in a multitasking environment. Basically,
you have to limit access to the file so that only one script can access
it at a time. How you do it is dependent on the OS.

Or you can use a database which is built for multitasking (most are).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171421 is a reply to message #171418] Sun, 02 January 2011 23:26 Go to previous messageGo to next message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma: 0
Junior Member
On Jan 2, 5:00 pm, Denis McMahon <denis.m.f.mcma...@googlemail.com>
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
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">
> <html>
> <head>
> <title>Yes-No Poll</title>
> </head>
> <body style="padding:1em 3em">
> <form method="post" action="quickpoll.php" name="f1" id="f1">
> <p>Yes: <input type="radio" name="vote" id="v1_1" value="1"><br>
> No: <input type="radio" name="vote" id="v1_0" value="0"><br>
> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
> </form>
> <p>The results so far:<br>
> Yes: $yes votes<br>
> No: $no votes</p>
> </body>
> </html>
> EOT;
> ?>
>
> Rgds
>
> Denis McMahon

Denis,

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. 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.

Thanks though.

Don
Re: Failed to write to a text file (text file is RW) [message #171422 is a reply to message #171421] Sun, 02 January 2011 23:51 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 02/01/11 23:26, justaguy wrote:

> 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. 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.

Shouldn't need execute access to write, can you check the logs and see
if there's a php error being recorded when you try and write the file?

Rgds

Denis McMahon
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 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
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)
Re: Failed to write to a text file (text file is RW) [message #171424 is a reply to message #171421] Sun, 02 January 2011 23:55 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/2/2011 6:26 PM, justaguy wrote:
> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
> 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
>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>> "http://www.w3.org/TR/html4/strict.dtd">
>> <html>
>> <head>
>> <title>Yes-No Poll</title>
>> </head>
>> <body style="padding:1em 3em">
>> <form method="post" action="quickpoll.php" name="f1" id="f1">
>> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
>> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
>> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
>> </form>
>> <p>The results so far:<br>
>> Yes: $yes votes<br>
>> No: $no votes</p>
>> </body>
>> </html>
>> EOT;
>> ?>
>>
>> Rgds
>>
>> Denis McMahon
>
> Denis,
>
> 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. 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.
>
> Thanks though.
>
> Don

You do not need execute privileges to write the file - in fact, you
should NOT have it - the file is not an executable. You also definitely
should NOT have global write (or read) privileges.

Did you check the return value from fopen()? ALWAYS check it to ensure
that the file opened correctly. Also, do you know if the fread() and/or
fwrite() worked correctly? You never check to see.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171425 is a reply to message #171418] Sun, 02 January 2011 23:56 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/2/2011 5:00 PM, 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
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">
> <html>
> <head>
> <title>Yes-No Poll</title>
> </head>
> <body style="padding:1em 3em">
> <form method="post" action="quickpoll.php" name="f1" id="f1">
> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
> </form>
> <p>The results so far:<br>
> Yes: $yes votes<br>
> No: $no votes</p>
> </body>
> </html>
> EOT;
> ?>
>
> Rgds
>
> Denis McMahon

Like the original op's, your code is totally unsafe in a multitasking
environment.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171426 is a reply to message #171424] Mon, 03 January 2011 01:04 Go to previous messageGo to next message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma: 0
Junior Member
On Jan 2, 6:55 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 6:26 PM, justaguy wrote:
>
>
>
>> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
>> 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
>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>>> "http://www.w3.org/TR/html4/strict.dtd">
>>> <html>
>>> <head>
>>> <title>Yes-No Poll</title>
>>> </head>
>>> <body style="padding:1em 3em">
>>> <form method="post" action="quickpoll.php" name="f1" id="f1">
>>> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
>>> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
>>> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
>>> </form>
>>> <p>The results so far:<br>
>>> Yes: $yes votes<br>
>>> No: $no votes</p>
>>> </body>
>>> </html>
>>> EOT;
>>> ?>
>
>>> Rgds
>
>>> Denis McMahon
>
>> Denis,
>
>> 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.  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.
>
>> Thanks though.
>
>> Don
>
> You do not need execute privileges to write the file - in fact, you
> should NOT have it - the file is not an executable.  You also definitely
> should NOT have global write (or read) privileges.
>
> Did you check the return value from fopen()?  ALWAYS check it to ensure
> that the file opened correctly.  Also, do you know if the fread() and/or
> fwrite() worked correctly?  You never check to see.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Jerry,

Here's the block of code with debugging info and it still has a
problem:
//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
echo "fp;";
echo $fp;

fputs($fp,$insertvote);
echo "insert; ";
echo $insertvote;
fclose($fp);

And here's its output:
10 fp;insert; 1||0

Which seems to suggest the operation of $fp = fopen($filename,"w")
failed.

// the poll_result.txt file is in the same directory
$filename = "poll_result.txt";

/* why ok with another php script?
Why a similar statement for file read in another script is ok.
$myFile = "someLog.txt";
*/

And yes, I know the 'x' bit isn't necessary. Odd. Thanks.
Re: Failed to write to a text file (text file is RW) [message #171427 is a reply to message #171426] Mon, 03 January 2011 01:34 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/2/2011 8:04 PM, justaguy wrote:
> On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>> On 1/2/2011 6:26 PM, justaguy wrote:
>>
>>
>>
>>> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
>>> 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
>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>>>> "http://www.w3.org/TR/html4/strict.dtd">
>>>> <html>
>>>> <head>
>>>> <title>Yes-No Poll</title>
>>>> </head>
>>>> <body style="padding:1em 3em">
>>>> <form method="post" action="quickpoll.php" name="f1" id="f1">
>>>> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
>>>> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
>>>> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
>>>> </form>
>>>> <p>The results so far:<br>
>>>> Yes: $yes votes<br>
>>>> No: $no votes</p>
>>>> </body>
>>>> </html>
>>>> EOT;
>>>> ?>
>>
>>>> Rgds
>>
>>>> Denis McMahon
>>
>>> Denis,
>>
>>> 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. 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.
>>
>>> Thanks though.
>>
>>> Don
>>
>> You do not need execute privileges to write the file - in fact, you
>> should NOT have it - the file is not an executable. You also definitely
>> should NOT have global write (or read) privileges.
>>
>> Did you check the return value from fopen()? ALWAYS check it to ensure
>> that the file opened correctly. Also, do you know if the fread() and/or
>> fwrite() worked correctly? You never check to see.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Jerry,
>
> Here's the block of code with debugging info and it still has a
> problem:
> //insert votes to txt file
> $insertvote = $yes."||".$no;
> $fp = fopen($filename,"w");
> echo "fp;";
> echo $fp;
>
> fputs($fp,$insertvote);
> echo "insert; ";
> echo $insertvote;
> fclose($fp);
>
> And here's its output:
> 10 fp;insert; 1||0
>
> Which seems to suggest the operation of $fp = fopen($filename,"w")
> failed.
>
> // the poll_result.txt file is in the same directory
> $filename = "poll_result.txt";
>
> /* why ok with another php script?
> Why a similar statement for file read in another script is ok.
> $myFile = "someLog.txt";
> */
>
> And yes, I know the 'x' bit isn't necessary. Odd. Thanks.

I'm not talking about 'debug code' - I'm talking about actual code you
should have in every script - error checking.

NEVER assume an external operation works. You should ALWAYS check the
return code, especially where external operations are concerned, and if
you get a bad one, you should take correct action.

fopen() returns false if the file was not opened. So your current
example should have checked for a failure in fopen and taken the
appropriate action. For testing purposes, displaying a message would be
sufficient. You would not want to do that on a production server, of
course, but you should at least log the error.

Had you had such code in place, you would have known that the fopen()
failed and could have followed down why. And if this is your
development server, you should have in your php.ini:

display_errors=on
error_reporting=E_ALL (or E_ALL | E_STRICT)

These would have given you additional messages. But you don't want these
on a production server; instead you should log your PHP errors to a file
and look at that file when you have problems.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171428 is a reply to message #171427] Mon, 03 January 2011 02:13 Go to previous messageGo to next message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma: 0
Junior Member
On Jan 2, 8:34 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 8:04 PM, justaguy wrote:
>
>
>
>> On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>>> On 1/2/2011 6:26 PM, justaguy wrote:
>
>>>> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
>>>> 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
>>>> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>>>> > "http://www.w3.org/TR/html4/strict.dtd">
>>>> > <html>
>>>> > <head>
>>>> > <title>Yes-No Poll</title>
>>>> > </head>
>>>> > <body style="padding:1em 3em">
>>>> > <form method="post" action="quickpoll.php" name="f1" id="f1">
>>>> > <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
>>>> > No:<input type="radio" name="vote" id="v1_0" value="0"><br>
>>>> > <input type="submit" value="Vote Now" name="s1" id="s1"></p>
>>>> > </form>
>>>> > <p>The results so far:<br>
>>>> > Yes: $yes votes<br>
>>>> > No: $no votes</p>
>>>> > </body>
>>>> > </html>
>>>> > EOT;
>>>> > ?>
>
>>>> > Rgds
>
>>>> > Denis McMahon
>
>>>> Denis,
>
>>>> 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.  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.
>
>>>> Thanks though.
>
>>>> Don
>
>>> You do not need execute privileges to write the file - in fact, you
>>> should NOT have it - the file is not an executable.  You also definitely
>>> should NOT have global write (or read) privileges.
>
>>> Did you check the return value from fopen()?  ALWAYS check it to ensure
>>> that the file opened correctly.  Also, do you know if the fread() and/or
>>> fwrite() worked correctly?  You never check to see.
>
>>> --
>>> ==================
>>> Remove the "x" from my email address
>>> Jerry Stuckle
>>> JDS Computer Training Corp.
>>> jstuck...@attglobal.net
>>> ==================
>
>> Jerry,
>
>> Here's the block of code with debugging info and it still has a
>> problem:
>> //insert votes to txt file
>> $insertvote = $yes."||".$no;
>> $fp = fopen($filename,"w");
>> echo "fp;";
>> echo $fp;
>
>> fputs($fp,$insertvote);
>> echo "insert; ";
>> echo $insertvote;
>> fclose($fp);
>
>> And here's its output:
>> 10 fp;insert; 1||0
>
>> Which seems to suggest the operation of $fp = fopen($filename,"w")
>> failed.
>
>> // the poll_result.txt file is in the same directory
>> $filename = "poll_result.txt";
>
>> /* why ok with another php script?
>> Why a similar statement for file read in another script is ok.
>> $myFile = "someLog.txt";
>> */
>
>> And yes, I know the 'x' bit isn't necessary.  Odd.   Thanks.
>
> I'm not talking about 'debug code' - I'm talking about actual code you
> should have in every script - error checking.
>
> NEVER assume an external operation works. You should ALWAYS check the
> return code, especially where external operations are concerned, and if
> you get a bad one, you should take correct action.
>
> fopen() returns false if the file was not opened.  So your current
> example should have checked for a failure in fopen and taken the
> appropriate action.  For testing purposes, displaying a message would be
> sufficient.  You would not want to do that on a production server, of
> course, but you should at least log the error.
>
> Had you had such code in place, you would have known that the fopen()
> failed and could have followed down why.  And if this is your
> development server, you should have in your php.ini:
>
> display_errors=on
> error_reporting=E_ALL  (or E_ALL | E_STRICT)
>
> These would have given you additional messages. But you don't want these
> on a production server; instead you should log your PHP errors to a file
> and look at that file when you have problems.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Jerry,

The code isn't working, so, of course it's not in production. I'm
using an ISP's server, don't have a lot of control...
yes, you're right, I was lazy in error detection/tracking for this
piece of code, thought it should be fairly straight-forward.

Thanks.

Don
Re: Failed to write to a text file (text file is RW) [message #171429 is a reply to message #171428] Mon, 03 January 2011 03:22 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/2/2011 9:13 PM, justaguy wrote:
> On Jan 2, 8:34 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>> On 1/2/2011 8:04 PM, justaguy wrote:
>>
>>
>>
>>> On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>>>> On 1/2/2011 6:26 PM, justaguy wrote:
>>
>>>> > On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
>>>> > 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
>>>> >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>>>> >> "http://www.w3.org/TR/html4/strict.dtd">
>>>> >> <html>
>>>> >> <head>
>>>> >> <title>Yes-No Poll</title>
>>>> >> </head>
>>>> >> <body style="padding:1em 3em">
>>>> >> <form method="post" action="quickpoll.php" name="f1" id="f1">
>>>> >> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
>>>> >> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
>>>> >> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
>>>> >> </form>
>>>> >> <p>The results so far:<br>
>>>> >> Yes: $yes votes<br>
>>>> >> No: $no votes</p>
>>>> >> </body>
>>>> >> </html>
>>>> >> EOT;
>>>> >> ?>
>>
>>>> >> Rgds
>>
>>>> >> Denis McMahon
>>
>>>> > Denis,
>>
>>>> > 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. 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.
>>
>>>> > Thanks though.
>>
>>>> > Don
>>
>>>> You do not need execute privileges to write the file - in fact, you
>>>> should NOT have it - the file is not an executable. You also definitely
>>>> should NOT have global write (or read) privileges.
>>
>>>> Did you check the return value from fopen()? ALWAYS check it to ensure
>>>> that the file opened correctly. Also, do you know if the fread() and/or
>>>> fwrite() worked correctly? You never check to see.
>>
>>>> --
>>>> ==================
>>>> Remove the "x" from my email address
>>>> Jerry Stuckle
>>>> JDS Computer Training Corp.
>>>> jstuck...@attglobal.net
>>>> ==================
>>
>>> Jerry,
>>
>>> Here's the block of code with debugging info and it still has a
>>> problem:
>>> //insert votes to txt file
>>> $insertvote = $yes."||".$no;
>>> $fp = fopen($filename,"w");
>>> echo "fp;";
>>> echo $fp;
>>
>>> fputs($fp,$insertvote);
>>> echo "insert; ";
>>> echo $insertvote;
>>> fclose($fp);
>>
>>> And here's its output:
>>> 10 fp;insert; 1||0
>>
>>> Which seems to suggest the operation of $fp = fopen($filename,"w")
>>> failed.
>>
>>> // the poll_result.txt file is in the same directory
>>> $filename = "poll_result.txt";
>>
>>> /* why ok with another php script?
>>> Why a similar statement for file read in another script is ok.
>>> $myFile = "someLog.txt";
>>> */
>>
>>> And yes, I know the 'x' bit isn't necessary. Odd. Thanks.
>>
>> I'm not talking about 'debug code' - I'm talking about actual code you
>> should have in every script - error checking.
>>
>> NEVER assume an external operation works. You should ALWAYS check the
>> return code, especially where external operations are concerned, and if
>> you get a bad one, you should take correct action.
>>
>> fopen() returns false if the file was not opened. So your current
>> example should have checked for a failure in fopen and taken the
>> appropriate action. For testing purposes, displaying a message would be
>> sufficient. You would not want to do that on a production server, of
>> course, but you should at least log the error.
>>
>> Had you had such code in place, you would have known that the fopen()
>> failed and could have followed down why. And if this is your
>> development server, you should have in your php.ini:
>>
>> display_errors=on
>> error_reporting=E_ALL (or E_ALL | E_STRICT)
>>
>> These would have given you additional messages. But you don't want these
>> on a production server; instead you should log your PHP errors to a file
>> and look at that file when you have problems.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Jerry,
>
> The code isn't working, so, of course it's not in production. I'm
> using an ISP's server, don't have a lot of control...
> yes, you're right, I was lazy in error detection/tracking for this
> piece of code, thought it should be fairly straight-forward.
>
> Thanks.
>
> Don

Another point - get your own development machine going. It will make
your life a lot easier. Even if it's not the same OS and won't catch
every error (i.e. you wouldn't see this error on Windows), it will help
you many other ways.

And depending on the ISP's setup, ini_set() and/or your .htaccess file
can change a number of PHP settings for a single script, every script on
your site, or many steps in between.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171430 is a reply to message #171425] Mon, 03 January 2011 03:38 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 02/01/11 23:56, Jerry Stuckle wrote:

> Like the original op's, your code is totally unsafe in a multitasking
> environment.

I didn't say it was. I just took his code and made it work on my dev
server. It's up to him to make sure his code is robust for the
environment he's running it in.

As my dev server isn't a multitasking environment from the point of view
of multiple users accessing the poll at once, I'm not too worried about
whether this particular code on it is robust in that environment.

Rgds

Denis McMahon
Re: Failed to write to a text file (text file is RW) [message #171431 is a reply to message #171430] Mon, 03 January 2011 03:46 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/2/2011 10:38 PM, Denis McMahon wrote:
> On 02/01/11 23:56, Jerry Stuckle wrote:
>
>> Like the original op's, your code is totally unsafe in a multitasking
>> environment.
>
> I didn't say it was. I just took his code and made it work on my dev
> server. It's up to him to make sure his code is robust for the
> environment he's running it in.
>
> As my dev server isn't a multitasking environment from the point of view
> of multiple users accessing the poll at once, I'm not too worried about
> whether this particular code on it is robust in that environment.
>
> Rgds
>
> Denis McMahon

No, but if you're going to suggest code, you need to suggest good code
or list major limitations like not being safe in a multitasking environment.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171432 is a reply to message #171429] Mon, 03 January 2011 04:29 Go to previous messageGo to next message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma: 0
Junior Member
On Jan 2, 10:22 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 9:13 PM, justaguy wrote:
>
>
>
>> On Jan 2, 8:34 pm, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>>> On 1/2/2011 8:04 PM, justaguy wrote:
>
>>>> On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net>    wrote:
>>>> > On 1/2/2011 6:26 PM, justaguy wrote:
>
>>>> >> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
>>>> >> 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
>>>> >>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>>>> >>> "http://www.w3.org/TR/html4/strict.dtd">
>>>> >>> <html>
>>>> >>> <head>
>>>> >>> <title>Yes-No Poll</title>
>>>> >>> </head>
>>>> >>> <body style="padding:1em 3em">
>>>> >>> <form method="post" action="quickpoll.php" name="f1" id="f1">
>>>> >>> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
>>>> >>> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
>>>> >>> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
>>>> >>> </form>
>>>> >>> <p>The results so far:<br>
>>>> >>> Yes: $yes votes<br>
>>>> >>> No: $no votes</p>
>>>> >>> </body>
>>>> >>> </html>
>>>> >>> EOT;
>>>> >>> ?>
>
>>>> >>> Rgds
>
>>>> >>> Denis McMahon
>
>>>> >> Denis,
>
>>>> >> 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.  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.
>
>>>> >> Thanks though.
>
>>>> >> Don
>
>>>> > You do not need execute privileges to write the file - in fact, you
>>>> > should NOT have it - the file is not an executable.  You also definitely
>>>> > should NOT have global write (or read) privileges.
>
>>>> > Did you check the return value from fopen()?  ALWAYS check it to ensure
>>>> > that the file opened correctly.  Also, do you know if the fread() and/or
>>>> > fwrite() worked correctly?  You never check to see.
>
>>>> > --
>>>> > ==================
>>>> > Remove the "x" from my email address
>>>> > Jerry Stuckle
>>>> > JDS Computer Training Corp.
>>>> > jstuck...@attglobal.net
>>>> > ==================
>
>>>> Jerry,
>
>>>> Here's the block of code with debugging info and it still has a
>>>> problem:
>>>> //insert votes to txt file
>>>> $insertvote = $yes."||".$no;
>>>> $fp = fopen($filename,"w");
>>>> echo "fp;";
>>>> echo $fp;
>
>>>> fputs($fp,$insertvote);
>>>> echo "insert; ";
>>>> echo $insertvote;
>>>> fclose($fp);
>
>>>> And here's its output:
>>>> 10 fp;insert; 1||0
>
>>>> Which seems to suggest the operation of $fp = fopen($filename,"w")
>>>> failed.
>
>>>> // the poll_result.txt file is in the same directory
>>>> $filename = "poll_result.txt";
>
>>>> /* why ok with another php script?
>>>> Why a similar statement for file read in another script is ok.
>>>> $myFile = "someLog.txt";
>>>> */
>
>>>> And yes, I know the 'x' bit isn't necessary.  Odd.   Thanks.
>
>>> I'm not talking about 'debug code' - I'm talking about actual code you
>>> should have in every script - error checking.
>
>>> NEVER assume an external operation works. You should ALWAYS check the
>>> return code, especially where external operations are concerned, and if
>>> you get a bad one, you should take correct action.
>
>>> fopen() returns false if the file was not opened.  So your current
>>> example should have checked for a failure in fopen and taken the
>>> appropriate action.  For testing purposes, displaying a message would be
>>> sufficient.  You would not want to do that on a production server, of
>>> course, but you should at least log the error.
>
>>> Had you had such code in place, you would have known that the fopen()
>>> failed and could have followed down why.  And if this is your
>>> development server, you should have in your php.ini:
>
>>> display_errors=on
>>> error_reporting=E_ALL  (or E_ALL | E_STRICT)
>
>>> These would have given you additional messages. But you don't want these
>>> on a production server; instead you should log your PHP errors to a file
>>> and look at that file when you have problems.
>
>>> --
>>> ==================
>>> Remove the "x" from my email address
>>> Jerry Stuckle
>>> JDS Computer Training Corp.
>>> jstuck...@attglobal.net
>>> ==================
>
>> Jerry,
>
>> The code isn't working, so, of course it's not in production.  I'm
>> using an ISP's server, don't have a lot of control...
>> yes, you're right, I was lazy in error detection/tracking for this
>> piece of code, thought it should be fairly straight-forward.
>
>> Thanks.
>
>> Don
>
> Another point - get your own development machine going. It will make
> your life a lot easier.  Even if it's not the same OS and won't catch
> every error (i.e. you wouldn't see this error on Windows), it will help
> you many other ways.
>
> And depending on the ISP's setup, ini_set() and/or your .htaccess file
> can change a number of PHP settings for a single script, every script on
> your site, or many steps in between.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Too bad, an open source webserver, Caucho Resin on my Windows 7 box
fails to support php. The same code produced a blank page with
nothing.
Re: Failed to write to a text file (text file is RW) [message #171433 is a reply to message #171432] Mon, 03 January 2011 06:36 Go to previous messageGo to next message
Adrienne Boswell is currently offline  Adrienne Boswell
Messages: 25
Registered: October 2010
Karma: 0
Junior Member
Gazing into my crystal ball I observed justaguy <donli(at)yahoo(dot)com>
writing in
news:ccc49c8d-262f-4b86-a1d6-4ef388fe677e(at)p7g2000prb(dot)googlegroups(dot)com:

> On Jan 2, 10:22 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> On 1/2/2011 9:13 PM, justaguy wrote:
>>
>>
>>
>>> On Jan 2, 8:34 pm, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>>>> On 1/2/2011 8:04 PM, justaguy wrote:
>>
>>>> > On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net>    wrot
> e:
>>>> >> On 1/2/2011 6:26 PM, justaguy wrote:
>>
>>>> >>> On Jan 2, 5:00 pm, Denis
>>>> >>> McMahon<denis.m.f.mcma...@googlemail.com> 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
>>>> >>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>>>> >>>> "http://www.w3.org/TR/html4/strict.dtd">
>>>> >>>> <html>
>>>> >>>> <head>
>>>> >>>> <title>Yes-No Poll</title>
>>>> >>>> </head>
>>>> >>>> <body style="padding:1em 3em">
>>>> >>>> <form method="post" action="quickpoll.php" name="f1" id="f
> 1">
>>>> >>>> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"
>> <br>
>>>> >>>> No:<input type="radio" name="vote" id="v1_0" value="0"><br
>>
>>>> >>>> <input type="submit" value="Vote Now" name="s1" id="s1"></
> p>
>>>> >>>> </form>
>>>> >>>> <p>The results so far:<br>
>>>> >>>> Yes: $yes votes<br>
>>>> >>>> No: $no votes</p>
>>>> >>>> </body>
>>>> >>>> </html>
>>>> >>>> EOT;
>>>> >>>> ?>
>>
>>>> >>>> Rgds
>>
>>>> >>>> Denis McMahon
>>
>>>> >>> Denis,
>>
>>>> >>> 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 whi
> le
>>>> >>> this one has rwrwrw attributes.  I thought the global writable
>>>> >>> wo
> uld
>>>> >>> suffice but it seems I was incorrect.  My ftp client failed to
>>>> >>> do chmod 755 to the global x (execute) attribute to the file.
>>
>>>> >>> Thanks though.
>>
>>>> >>> Don
>>
>>>> >> You do not need execute privileges to write the file - in fact,
>>>> >> you should NOT have it - the file is not an executable.  You
>>>> >> also defi
> nitely
>>>> >> should NOT have global write (or read) privileges.
>>
>>>> >> Did you check the return value from fopen()?  ALWAYS check it to
>>>> >> e
> nsure
>>>> >> that the file opened correctly.  Also, do you know if the
>>>> >> fread()
> and/or
>>>> >> fwrite() worked correctly?  You never check to see.
>>
>>>> >> --
>>>> >> =================
>>>> >> Remove the "x" from my email address
>>>> >> Jerry Stuckle
>>>> >> JDS Computer Training Corp.
>>>> >> jstuck...@attglobal.net
>>>> >> =================
>>
>>>> > Jerry,
>>
>>>> > Here's the block of code with debugging info and it still has a
>>>> > problem:
>>>> > //insert votes to txt file
>>>> > $insertvote = $yes."||".$no;
>>>> > $fp = fopen($filename,"w");
>>>> > echo "fp;";
>>>> > echo $fp;
>>
>>>> > fputs($fp,$insertvote);
>>>> > echo "insert; ";
>>>> > echo $insertvote;
>>>> > fclose($fp);
>>
>>>> > And here's its output:
>>>> > 10 fp;insert; 1||0
>>
>>>> > Which seems to suggest the operation of $fp =
>>>> > fopen($filename,"w") failed.
>>
>>>> > // the poll_result.txt file is in the same directory
>>>> > $filename = "poll_result.txt";
>>
>>>> > /* why ok with another php script?
>>>> > Why a similar statement for file read in another script is ok.
>>>> > $myFile = "someLog.txt";
>>>> > */
>>
>>>> > And yes, I know the 'x' bit isn't necessary.  Odd.   Thanks.
>>
>>>> I'm not talking about 'debug code' - I'm talking about actual code
>>>> you should have in every script - error checking.
>>
>>>> NEVER assume an external operation works. You should ALWAYS check
>>>> the return code, especially where external operations are
>>>> concerned, and i
> f
>>>> you get a bad one, you should take correct action.
>>
>>>> fopen() returns false if the file was not opened.  So your current
>>>> example should have checked for a failure in fopen and taken the
>>>> appropriate action.  For testing purposes, displaying a message
>>>> woul
> d be
>>>> sufficient.  You would not want to do that on a production server,
>>>> o
> f
>>>> course, but you should at least log the error.
>>
>>>> Had you had such code in place, you would have known that the
>>>> fopen() failed and could have followed down why.  And if this is
>>>> your development server, you should have in your php.ini:
>>
>>>> display_errors=on
>>>> error_reporting=E_ALL  (or E_ALL | E_STRICT)
>>
>>>> These would have given you additional messages. But you don't want
>>>> the
> se
>>>> on a production server; instead you should log your PHP errors to
>>>> a fi
> le
>>>> and look at that file when you have problems.
>>
>>>> --
>>>> =================
>>>> Remove the "x" from my email address
>>>> Jerry Stuckle
>>>> JDS Computer Training Corp.
>>>> jstuck...@attglobal.net
>>>> =================
>>
>>> Jerry,
>>
>>> The code isn't working, so, of course it's not in production.  I'm
>>> using an ISP's server, don't have a lot of control...
>>> yes, you're right, I was lazy in error detection/tracking for this
>>> piece of code, thought it should be fairly straight-forward.
>>
>>> Thanks.
>>
>>> Don
>>
>> Another point - get your own development machine going. It will make
>> your life a lot easier.  Even if it's not the same OS and won't catch
>> every error (i.e. you wouldn't see this error on Windows), it will
>> help you many other ways.
>>
>> And depending on the ISP's setup, ini_set() and/or your .htaccess
>> file can change a number of PHP settings for a single script, every
>> script on your site, or many steps in between.
>>
>> --
>> =================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> =================
>
> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
> fails to support php. The same code produced a blank page with
> nothing.
>

What edition of Win7? If it's not Home, you could use IIS.

--
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share
Re: Failed to write to a text file (text file is RW) [message #171435 is a reply to message #171432] Mon, 03 January 2011 11:53 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/2/2011 11:29 PM, justaguy wrote:
>>
>>> Jerry,
>>
>>> The code isn't working, so, of course it's not in production. I'm
>>> using an ISP's server, don't have a lot of control...
>>> yes, you're right, I was lazy in error detection/tracking for this
>>> piece of code, thought it should be fairly straight-forward.
>>
>>> Thanks.
>>
>>> Don
>>
>> Another point - get your own development machine going. It will make
>> your life a lot easier. Even if it's not the same OS and won't catch
>> every error (i.e. you wouldn't see this error on Windows), it will help
>> you many other ways.
>>
>> And depending on the ISP's setup, ini_set() and/or your .htaccess file
>> can change a number of PHP settings for a single script, every script on
>> your site, or many steps in between.
>>
>
> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
> fails to support php. The same code produced a blank page with
> nothing.

Why don't you just install Apache - it's open source, free and supports
PHP just fine. It also happens to be the most used web server in the
world.

And BTW - I didn't say the code was "in production". I said it was "on
a production machine". Two entirely different things.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171437 is a reply to message #171431] Mon, 03 January 2011 13:36 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 03/01/11 03:46, Jerry Stuckle wrote:

> No, but if you're going to suggest code, you need to suggest good code
> or list major limitations like not being safe in a multitasking
> environment.

I see no point in voicing issues you've already mentioned Jerry.

Rgds

Denis McMahon
Re: Failed to write to a text file (text file is RW) [message #171438 is a reply to message #171432] Mon, 03 January 2011 13:38 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 03/01/11 04:29, justaguy wrote:

> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
> fails to support php. The same code produced a blank page with
> nothing.

If you want a PHP / MySQL / Apache setup on a windows box for code
development, check out http://www.wampserver.com

Rgds

Denis McMahon
Re: Failed to write to a text file (text file is RW) [message #171444 is a reply to message #171435] Mon, 03 January 2011 23:11 Go to previous messageGo to next message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma: 0
Junior Member
On Jan 3, 6:53 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 11:29 PM, justaguy wrote:
>
>
>
>
>
>>>> Jerry,
>
>>>> The code isn't working, so, of course it's not in production.  I'm
>>>> using an ISP's server, don't have a lot of control...
>>>> yes, you're right, I was lazy in error detection/tracking for this
>>>> piece of code, thought it should be fairly straight-forward.
>
>>>> Thanks.
>
>>>> Don
>
>>> Another point - get your own development machine going. It will make
>>> your life a lot easier.  Even if it's not the same OS and won't catch
>>> every error (i.e. you wouldn't see this error on Windows), it will help
>>> you many other ways.
>
>>> And depending on the ISP's setup, ini_set() and/or your .htaccess file
>>> can change a number of PHP settings for a single script, every script on
>>> your site, or many steps in between.
>
>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>> fails to support php.  The same code produced a blank page with
>> nothing.
>
> Why don't you just install Apache - it's open source, free and supports
> PHP just fine.   It also happens to be the most used web server in the
> world.
>
> And BTW - I didn't say the code was "in production".  I said it was "on
> a production machine".  Two entirely different things.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Hi all, problem resolved with the same (my 'original' code). My ISP
first adversely changed file attributes of another text file,
Terrible! Which failed my software download routine, anyway, after
calling them up and instructing them to set it to rw rw rw, all's well
now.

But let me ask Jerry and all, how do we ensure the "target file" to
support concurrent users, if it's opened by user A, we need to lock it
or something... how do we do it with php?

Many thanks.

Don
Re: Failed to write to a text file (text file is RW) [message #171445 is a reply to message #171438] Mon, 03 January 2011 23:12 Go to previous messageGo to next message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma: 0
Junior Member
On Jan 3, 8:38 am, Denis McMahon <denis.m.f.mcma...@googlemail.com>
wrote:
> On 03/01/11 04:29, justaguy wrote:
>
>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>> fails to support php.  The same code produced a blank page with
>> nothing.
>
> If you want a PHP / MySQL / Apache setup on a windows box for code
> development, check outhttp://www.wampserver.com
>
> Rgds
>
> Denis McMahon

I can't, Denis, if I do it must mirror what my ISP has. Thanks
though. Don
Re: Failed to write to a text file (text file is RW) [message #171447 is a reply to message #171444] Tue, 04 January 2011 00:37 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/3/2011 6:11 PM, justaguy wrote:
> On Jan 3, 6:53 am, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>> On 1/2/2011 11:29 PM, justaguy wrote:
>>
>>
>>
>>
>>
>>>> > Jerry,
>>
>>>> > The code isn't working, so, of course it's not in production. I'm
>>>> > using an ISP's server, don't have a lot of control...
>>>> > yes, you're right, I was lazy in error detection/tracking for this
>>>> > piece of code, thought it should be fairly straight-forward.
>>
>>>> > Thanks.
>>
>>>> > Don
>>
>>>> Another point - get your own development machine going. It will make
>>>> your life a lot easier. Even if it's not the same OS and won't catch
>>>> every error (i.e. you wouldn't see this error on Windows), it will help
>>>> you many other ways.
>>
>>>> And depending on the ISP's setup, ini_set() and/or your .htaccess file
>>>> can change a number of PHP settings for a single script, every script on
>>>> your site, or many steps in between.
>>
>>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>>> fails to support php. The same code produced a blank page with
>>> nothing.
>>
>> Why don't you just install Apache - it's open source, free and supports
>> PHP just fine. It also happens to be the most used web server in the
>> world.
>>
>> And BTW - I didn't say the code was "in production". I said it was "on
>> a production machine". Two entirely different things.
>>
>
> Hi all, problem resolved with the same (my 'original' code). My ISP
> first adversely changed file attributes of another text file,
> Terrible! Which failed my software download routine, anyway, after
> calling them up and instructing them to set it to rw rw rw, all's well
> now.
>
> But let me ask Jerry and all, how do we ensure the "target file" to
> support concurrent users, if it's opened by user A, we need to lock it
> or something... how do we do it with php?
>
> Many thanks.
>
> Don

It should NOT be world writable, which is one security exposure.
Another is that anyone can download the file at any time, because it's
in your web site's root directory. Both are bad. At the very least you
should protect access through .htaccess, and better is to have it
outside your web site's root.

You can single thread access on some OS's - see flock() in the PHP
documentation. But better is to use a database, which will solve all
these problems.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171448 is a reply to message #171445] Tue, 04 January 2011 00:38 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/3/2011 6:12 PM, justaguy wrote:
> On Jan 3, 8:38 am, Denis McMahon<denis.m.f.mcma...@googlemail.com>
> wrote:
>> On 03/01/11 04:29, justaguy wrote:
>>
>>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>>> fails to support php. The same code produced a blank page with
>>> nothing.
>>
>> If you want a PHP / MySQL / Apache setup on a windows box for code
>> development, check outhttp://www.wampserver.com
>>
>> Rgds
>>
>> Denis McMahon
>
> I can't, Denis, if I do it must mirror what my ISP has. Thanks
> though. Don

Not a problem, if you code your scripts correctly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171449 is a reply to message #171444] Tue, 04 January 2011 04:16 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
justaguy wrote:
> On Jan 3, 6:53 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> On 1/2/2011 11:29 PM, justaguy wrote:
>>
>>
>>
>>
>>
>>>> > Jerry,
>>>> > The code isn't working, so, of course it's not in production. I'm
>>>> > using an ISP's server, don't have a lot of control...
>>>> > yes, you're right, I was lazy in error detection/tracking for this
>>>> > piece of code, thought it should be fairly straight-forward.
>>>> > Thanks.
>>>> > Don
>>>> Another point - get your own development machine going. It will make
>>>> your life a lot easier. Even if it's not the same OS and won't catch
>>>> every error (i.e. you wouldn't see this error on Windows), it will help
>>>> you many other ways.
>>>> And depending on the ISP's setup, ini_set() and/or your .htaccess file
>>>> can change a number of PHP settings for a single script, every script on
>>>> your site, or many steps in between.
>>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>>> fails to support php. The same code produced a blank page with
>>> nothing.
>> Why don't you just install Apache - it's open source, free and supports
>> PHP just fine. It also happens to be the most used web server in the
>> world.
>>
>> And BTW - I didn't say the code was "in production". I said it was "on
>> a production machine". Two entirely different things.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Hi all, problem resolved with the same (my 'original' code). My ISP
> first adversely changed file attributes of another text file,
> Terrible! Which failed my software download routine, anyway, after
> calling them up and instructing them to set it to rw rw rw, all's well
> now.
>
> But let me ask Jerry and all, how do we ensure the "target file" to
> support concurrent users, if it's opened by user A, we need to lock it
> or something... how do we do it with php?
>
> Many thanks.
>
> Don
create a lock file
Re: Failed to write to a text file (text file is RW) [message #171475 is a reply to message #171447] Wed, 05 January 2011 03:19 Go to previous messageGo to next message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma: 0
Junior Member
On Jan 3, 7:37 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/3/2011 6:11 PM, justaguy wrote:
>
>
>
>> On Jan 3, 6:53 am, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>>> On 1/2/2011 11:29 PM, justaguy wrote:
>
>>>> >> Jerry,
>
>>>> >> The code isn't working, so, of course it's not in production.  I'm
>>>> >> using an ISP's server, don't have a lot of control...
>>>> >> yes, you're right, I was lazy in error detection/tracking for this
>>>> >> piece of code, thought it should be fairly straight-forward.
>
>>>> >> Thanks.
>
>>>> >> Don
>
>>>> > Another point - get your own development machine going. It will make
>>>> > your life a lot easier.  Even if it's not the same OS and won't catch
>>>> > every error (i.e. you wouldn't see this error on Windows), it will help
>>>> > you many other ways.
>
>>>> > And depending on the ISP's setup, ini_set() and/or your .htaccess file
>>>> > can change a number of PHP settings for a single script, every script on
>>>> > your site, or many steps in between.
>
>>>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>>>> fails to support php.  The same code produced a blank page with
>>>> nothing.
>
>>> Why don't you just install Apache - it's open source, free and supports
>>> PHP just fine.   It also happens to be the most used web server in the
>>> world.
>
>>> And BTW - I didn't say the code was "in production".  I said it was "on
>>> a production machine".  Two entirely different things.
>
>> Hi all, problem resolved with the same (my 'original' code).  My ISP
>> first adversely changed file attributes of another text file,
>> Terrible!  Which failed my software download routine, anyway, after
>> calling them up and instructing them to set it to rw rw rw, all's well
>> now.
>
>> But let me ask Jerry and all, how do we ensure the "target file" to
>> support concurrent users, if it's opened by user A, we need to lock it
>> or something... how do we do it with php?
>
>> Many thanks.
>
>> Don
>
> It should NOT be world writable, which is one security exposure.
> Another is that anyone can download the file at any time, because it's
> in your web site's root directory.  Both are bad.  At the very least you
> should protect access through .htaccess, and better is to have it
> outside your web site's root.
>
> You can single thread access on some OS's - see flock() in the PHP
> documentation.  But better is to use a database, which will solve all
> these problems.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================


"
It should NOT be world writable,
"
world writable means to let Internet users to write to it, so, the
global w attribute is removed, how could an anonymous user
write to it?


"and better is to have it outside your web site's root. "
Yes, for now it's for dev purpose, so, I didn't care.

"
You can single thread access on some OS's - see flock() in the PHP
documentation.
"
sample code? Since I'm not a php coder and don't intend to become
one, at least not now... could you save me some time?

"
But better is to use a database, which will solve all these problems.
"
No, not practical, this is a temp task (a poll).

Thanks.
Re: Failed to write to a text file (text file is RW) [message #171476 is a reply to message #171475] Wed, 05 January 2011 03:54 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/4/2011 10:19 PM, justaguy wrote:
> On Jan 3, 7:37 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>> On 1/3/2011 6:11 PM, justaguy wrote:
>>
>>
>>
>>> On Jan 3, 6:53 am, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>>>> On 1/2/2011 11:29 PM, justaguy wrote:
>>
>>>> >>> Jerry,
>>
>>>> >>> The code isn't working, so, of course it's not in production. I'm
>>>> >>> using an ISP's server, don't have a lot of control...
>>>> >>> yes, you're right, I was lazy in error detection/tracking for this
>>>> >>> piece of code, thought it should be fairly straight-forward.
>>
>>>> >>> Thanks.
>>
>>>> >>> Don
>>
>>>> >> Another point - get your own development machine going. It will make
>>>> >> your life a lot easier. Even if it's not the same OS and won't catch
>>>> >> every error (i.e. you wouldn't see this error on Windows), it will help
>>>> >> you many other ways.
>>
>>>> >> And depending on the ISP's setup, ini_set() and/or your .htaccess file
>>>> >> can change a number of PHP settings for a single script, every script on
>>>> >> your site, or many steps in between.
>>
>>>> > Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>>>> > fails to support php. The same code produced a blank page with
>>>> > nothing.
>>
>>>> Why don't you just install Apache - it's open source, free and supports
>>>> PHP just fine. It also happens to be the most used web server in the
>>>> world.
>>
>>>> And BTW - I didn't say the code was "in production". I said it was "on
>>>> a production machine". Two entirely different things.
>>
>>> Hi all, problem resolved with the same (my 'original' code). My ISP
>>> first adversely changed file attributes of another text file,
>>> Terrible! Which failed my software download routine, anyway, after
>>> calling them up and instructing them to set it to rw rw rw, all's well
>>> now.
>>
>>> But let me ask Jerry and all, how do we ensure the "target file" to
>>> support concurrent users, if it's opened by user A, we need to lock it
>>> or something... how do we do it with php?
>>
>>> Many thanks.
>>
>>> Don
>>
>> It should NOT be world writable, which is one security exposure.
>> Another is that anyone can download the file at any time, because it's
>> in your web site's root directory. Both are bad. At the very least you
>> should protect access through .htaccess, and better is to have it
>> outside your web site's root.
>>
>> You can single thread access on some OS's - see flock() in the PHP
>> documentation. But better is to use a database, which will solve all
>> these problems.
>>
>
>
> "
> It should NOT be world writable,
> "
> world writable means to let Internet users to write to it, so, the
> global w attribute is removed, how could an anonymous user
> write to it?
>

No, world writable means anyone else on that server can write to it -
including other users. You may have 150 web sites on a shared server,
with each web site having its own users. Any of them can overwrite your
file.

>
> "and better is to have it outside your web site's root."
> Yes, for now it's for dev purpose, so, I didn't care.
>

Doesn't matter. If you start with good habits, you have fewer problems
later.

> "
> You can single thread access on some OS's - see flock() in the PHP
> documentation.
> "
> sample code? Since I'm not a php coder and don't intend to become
> one, at least not now... could you save me some time?
>

At my normal consulting rates, I can. This newsgroup is for users
helping users, not free consulting.

> "
> But better is to use a database, which will solve all these problems.
> "
> No, not practical, this is a temp task (a poll).
>
> Thanks.
>

Still better to use a database - and completely practical.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Failed to write to a text file (text file is RW) [message #171478 is a reply to message #171475] Wed, 05 January 2011 08:47 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 05/01/11 03:19, justaguy wrote:

> "
> It should NOT be world writable,
> "
> world writable means to let Internet users to write to it, so, the
> global w attribute is removed, how could an anonymous user
> write to it?

No. The file system access attributes control access for the server file
system. Internet users are not logged on to the server as users. The web
server process is the user that is accessing the file.

eg If your web server process runs as user www_data then www_data will
be the owner of any file it creates, and will only need owner rw access
to read and write the file.

> "
> You can single thread access on some OS's - see flock() in the PHP
> documentation.
> "
> sample code? Since I'm not a php coder and don't intend to become
> one, at least not now... could you save me some time?

There's usually plenty of code examples in the user added notes to the
php manual. Go read.

> "
> But better is to use a database, which will solve all these problems.
> "
> No, not practical, this is a temp task (a poll).

The database functionality may already be there, simply waiting for you
to take advantage of it.

Rgds

Denis McMahon
Re: Failed to write to a text file (text file is RW) [message #171481 is a reply to message #171475] Wed, 05 January 2011 13:00 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 05/01/11 03:19, justaguy wrote:

> "
> You can single thread access on some OS's - see flock() in the PHP
> documentation.
> "
> sample code? Since I'm not a php coder and don't intend to become
> one, at least not now... could you save me some time?

This isn't perfect, but should allow you to output votes to a file that
you can tally later. If the file can't be seized for append, it backs
off up to 10 times for a random interval of 0.1 +- 0.05 secs and tries
again. It may be more robust than your approach. Doubtless others here
will pick it apart, but they prefer to do that rather than offer solutions.

<?php

// Use one of these file names as appropriate to your setup
$file = "/home/justaguy/path/poll_result.log";
$file = "data/poll_result.log";
$file = "poll_result.log";

$v = "no";
if ($vote == 1) $v = "yes";
$info = $_SERVER['REMOTE_ADDR'] . " " . date(DATE_RFC2822);

$vstr = $v . " " . $info . "\n";

$tries = 10;
while (!$fh = @fopen($file,"a") && $tries--) usleep(rand(50000,150000));
if ($fh) {
fwrite($fh,$vstr);
fclose($fh);
}
else {
error_log($info . ' open poll_result.log for append failed.');
}

?>

If after 10 attempts it still fails, it puts a message in wherever the
error log is defined as.

Code to open, read and tally the votes is left as an exercise for the OP.

Note, however, that this won't solve your original problem, which seems
to be that the process that is executing the php doesn't have write
access to the directory that the file is being created in.

Rgds

Denis McMahon
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Print PHP Manual
Next Topic: Fetching an external web page
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 01:15:48 GMT 2024

Total time taken to generate the page: 0.02229 seconds