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
Return to the default flat view Create a new topic Submit Reply
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 previous message
justaguy is currently offline  justaguy
Messages: 16
Registered: December 2010
Karma:
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.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Print PHP Manual
Next Topic: Fetching an external web page
Goto Forum:
  

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

Current Time: Sat Nov 30 10:25:34 GMT 2024

Total time taken to generate the page: 0.04240 seconds