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 #171419 is a reply to message #171416] Sun, 02 January 2011 22:13 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
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
==================
[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 23 07:17:54 GMT 2024

Total time taken to generate the page: 0.04252 seconds