title: How to handle multiple request to write a file in php?
body: <p>I have this code</p>
  
  <pre><code>&lt;?php
      if (!file_exists($folderAddress . $_GET['name'] . '.json')) {
              //create file
              $myJson = fopen($folder.$_GET['name']. ".json", "w");        
              //get a context of a url 
              $ch = curl_init($myUrl);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              $text = '';
              if( ($text = curl_exec($ch) ) === false)
              {
                  die('fail');
              }
              // Close handle
              curl_close($ch); 
              //copy to json file
              $result = fwrite($myJson, $text);
              fclose($myJson);
              $t = time();
             //add update date to db
              if (!mysqli_query($con, "INSERT INTO test (name )
              VALUES ('$_GET['name]', '$t')")
              ) {
              echo $text;
              mysqli_close($con);
                  die('');
              }
              //show the json file
              echo $text;
          } 
  ?&gt;
  </code></pre>
  
  <p>And i have this problem if users request this file in same time or with less than a 500 ms delay all of them think that the file does not exist. So how can i prevent users to write file when the first is writing on it  ?</p>