Re: How to "synchronize" file write accesses



On Jan 13, 11:23 pm, "Netx" <user72cut-it-...@xxxxxx> wrote:
My .php script checks if the "_copper.txt" file is not older than
1h.
If it is older than the content of "_copper.txt" is
imported/updated from the other web page.
Then the content of "_copper.txt" file is displayed.

So in need of "update" I have the following fragment :
 $myFile = '_copper.txt';
 $fh = fopen($myFile, 'w') or die('can\'t open _copper.txt');
 //--CODE TO IMPORT COPPER PRICE FROM WWW
 //(...)
 fwrite($fh,$copper1);
 fclose($fh);

However I am afraid what will happen if two persons request the
page and two "threads"  fire fopen() for write? I can get over
problems
with one badly processed request, but I am afraid that
"_copper.txt" file may be damaged what will lead to all
subsequent requests badly displaying copper price.
How would you suggest to correct it and if possible without
database?

Mark

function getCopper()
{
    $filename = '_copper.txt';
    $filemtime = filemtime($filename);
    $currenttime = mktime();
    $diff = $currenttime - $filemtime;
    //seconds to refresh:
    if ($diff > 3600) {
     getCopperReally();
    }
    $fh = fopen($filename,'r') or die ('can\'t open
_copper.txt');
    $theData = fread($fh, filesize($filename));
    fclose($fh);
    print ($theData);

}

function getCopperReally()
{
 $myFile = '_copper.txt';
 $fh = fopen($myFile, 'w') or die('can\'t open _copper.txt');
 $result=get_web_page('www-address-goes-here');

 //process the page to get copper price into $copper1
 //(it works)

 fwrite($fh,$copper1);
 fclose($fh);

}



.



Relevant Pages

  • Re: How to "synchronize" file write accesses
    ... However I am afraid what will happen if two persons request the ... subsequent requests badly displaying copper price. ... Rob/Netx - use a database - mysql is free and easy to configure and will solve these unworkable ideas you have conjured up... ...
    (comp.lang.php)
  • Re: How to "synchronize" file write accesses
    ... However I am afraid what will happen if two persons request the ... subsequent requests badly displaying copper price. ... function getCopper() ...
    (comp.lang.php)
  • Re: How to "synchronize" file write accesses
    ... However I am afraid what will happen if two persons request the ... subsequent requests badly displaying copper price. ... Create _copper.new and rename it to _copper.txt ...
    (comp.lang.php)
  • Re: How to "synchronize" file write accesses
    ... However I am afraid what will happen if two persons request the ... subsequent requests badly displaying copper price. ...     fclose; ...
    (comp.lang.php)
  • Re: Need help with a complex data set
    ... time from a request to ship an order and the time it actually ships. ...   tblOrderRequests.VisualOrderID, ... next day then that first request was fullfilled. ...
    (microsoft.public.access.queries)