title: PHP Issue with using a temporary file for cache of content
body: <p>I have been working on creating a quick twitter widget for myself to re use, just something simple that caches the data from the public api. Since twitter only allows X requests for a certain time period  and I have been testing this on a shared host, Its very often that I run out of requests and twitter denies my request. Thus I check first whether my request is denied, before writing an updated cached file.</p>
  
  <p>Unfortunately I seem to lose this file from time to time, as I often see the 'temp file not written' message. Which should only appear if the file doesn't exist.</p>
  
  <p>Here is the full php function: </p>
  
  <pre><code>function getTweets($num)
  {
      $cfile = sys_get_temp_dir().'/e1z'. $type . md5 ( 'something' );
  
      if (is_file ( $cfile ) == false) {
          $cfile_time = strtotime ( '1983-04-30 07:15:00' );
      } else {
          $cfile_time = filemtime ( $cfile );
      }    
  
      $difference = strtotime ( date ( 'Y-m-d H:i:s' ) ) - $cfile_time;
  
      if ($difference &gt;= 100) {
  
          $tags = array("created_at", "text", "screen_name", "profile_image_url"); // twitter names
          $local = array("time", "msg", "user", "image"); // local names
  
          $reader = new XMLReader();
          $url = 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=boriskourt&amp;include_rts=true&amp;count=' . $num;
  
          $headers = get_headers($url, 1);
          if ($headers[0] == 'HTTP/1.0 400 Bad Request'){
  
                  if (is_file ( $cfile ) == true) {
                          $returner = file_get_contents ( $cfile );                   
                          touch ( $cfile );
                          file_put_contents ( $cfile, strval($returner) );
                          $returner = file_get_contents ( $cfile );
                          return  $returner;
                  } else {
                          $returner = "&lt;li&gt;&lt;span&gt;Temp file not written&lt;/span&gt;&lt;/li&gt;";
                          return  $returner;
                  }
  
          } else { 
              $reader-&gt;open($url);
  
              $i = 0;
              $k = 1;
  
              while ($i &lt; $num)
              {
                  $j = 0;
  
                  while ($reader-&gt;read() &amp;&amp; $j &lt; 4) // run through each tweet
                  {
                      if ($reader-&gt;nodeType == XMLReader::ELEMENT &amp;&amp; $reader-&gt;name == $tags[$j])
                      {
                          while ($reader-&gt;read())
                          {
                              if ($reader-&gt;nodeType == XMLReader::TEXT &amp;&amp; $j == 0)
                              {
                                  if ($k) {
                                  $tweets[$i][$local[$j]] = $reader-&gt;value;
                                  $j++;
                                  $k=0;}
                                  else {$k=1;}
                                  break;
                              }
                              else if ($reader-&gt;nodeType == XMLReader::TEXT)
                              {
                                  $tweets[$i][$local[$j]] = $reader-&gt;value;
                                  $j++;
                                  break;
                              }
                          }
                      }
                  }
  
                  $i++;
              }
  
              $returner = "";
  
              foreach ($tweets as $value) {
                  if ($value[user] != 'fugataquintet') {
                      $returner .= '&lt;li class="retweet"&gt;';
                  } else {
                      $returner .= '&lt;li&gt;';
                  }
                  $messager = $value[msg];
                  $messager = " ".preg_replace( "/(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]*)"."([[:alnum:]#?\/&amp;=])/i", "&lt;a href=\"\\1\\3\\4\" target=\"_blank\"&gt;"."\\1\\3\\4&lt;/a&gt;", $messager);
                  $messager =  preg_replace( "/ +@([a-z0-9_]*) ?/i", " &lt;a href=\"http://twitter.com/#!/\\1\" target=\"_blank\"&gt;@\\1&lt;/a&gt; ", $messager);
                  $messager = preg_replace( "/ +#([a-z0-9_]*) ?/i", " &lt;a href=\"http://twitter.com/search?q=%23\\1\" target=\"_blank\"&gt;#\\1&lt;/a&gt; ", $messager);
                  $returner .= '&lt;span&gt;'.$messager.'&lt;/span&gt;&lt;a class="datereplace" href="http://twitter.com/#!/fugataquintet" title="'.$value[time].'"&gt;'.$value[time].'&lt;/a&gt;&lt;/li&gt;';
              }
  
              touch ( $cfile );
              file_put_contents ( $cfile, strval($returner) );
              return  $returner;    
          }    
      } else {    
          $returner = file_get_contents ( $cfile );
          return  $returner;    
      }
  }
  </code></pre>