title: php chmod not working on newly created files on Azure Web App
body: <p>I am attempting to migrate an application built with CodeIgniter from a data center hosted server to run on an Azure Web App service. I've had to work through a few issues already but this one has us stumped . . .</p>
  
  <p>With CodeIgniter, the CI_Log class construct method calls a function called is_really_writable in the system/core/common.php file to make sure that the log directory is writable. This function creates a file in the given folder, then attempts to execute chmod on the file to set permissions then deletes it. If it is able to complete these actions it assumes it can write to the directory.</p>
  
  <p>I am getting the following error in my log files when this is called:</p>
  
  <blockquote>
    <p>ERROR - 2016-11-02 12:19:42 --> Severity: Warning  --> chmod(): No such file or directory D:\home\site\wwwroot\system\core\Common.php 92</p>
    
    <p>ERROR - 2016-11-02 12:19:42 --> Severity: Warning  --> unlink(application/logs/1f3202d820180a39f736f20fce790de8): No such file or directory D:\home\site\wwwroot\system\core\Common.php 93</p>
  </blockquote>
  
  <p>Then my log folder has my normal log files, and a lot of garbage files like the "1f3202d820180a39f736f20fce790de8" above. It seems that this function has to be returning true sometimes because it does write a log file (which it would not do if the function were to return false).</p>
  
  <p>My only ideas are:
   - It could be something to do with permissions on Azure. I find this to be unlikely because it must be successful at least a few times because the logs are getting created and written to.
   - Maybe the server is just really slow and the file is not created before attempting to chmod and then unlink. I find this to be really unlikely though because I would think that the fopen function would not return until the file was created.</p>
  
  <p>I am at my wits end and just about ready to give up trying to use Azure Web App service if I can't get this working. Any ideas?</p>
  
  <p>For reference, here is the code of the function in question:</p>
  
  <pre><code>function is_really_writable($file)
  {
      // If we're on a Unix server with safe_mode off we call is_writable
      if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") != TRUE)
      {
          return is_writable($file);
      }
  
      // For windows servers and safe_mode "on" installations we'll actually
      // write a file then read it.  Bah...
      if (is_dir($file))
      {
          $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
  
          if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
          {
              return FALSE;
          }
  
          fclose($fp);
          @chmod($file, DIR_WRITE_MODE);
          @unlink($file);
          return TRUE;
      }
      elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
      {
          return FALSE;
      }
  
      fclose($fp);
      return TRUE;
  }
  </code></pre>
  
  <p>And here is the construct method of the log class that is calling it:</p>
  
  <pre><code>public function __construct()
  {
      $config =&amp; get_config();
  
      $this-&gt;_log_path = ($config['log_path'] != '') ? $config['log_path'] : APPPATH.'logs/';
  
      if ( ! is_dir($this-&gt;_log_path) OR ! is_really_writable($this-&gt;_log_path))
      {
          $this-&gt;_enabled = FALSE;
      }
  
      if (is_numeric($config['log_threshold']))
      {
          $this-&gt;_threshold = $config['log_threshold'];
      }
  
      if ($config['log_date_format'] != '')
      {
          $this-&gt;_date_fmt = $config['log_date_format'];
      }
  }
  </code></pre>
  
  <p>Per <a href="https://stackoverflow.com/users/4989668/gary-liu-msft">gary-liu-msft</a> suggestion, I have done some more work to isolate this as just a problem with CodeIgniter and I am 100% sure it is just the version of CodeIgniter we are using at this point. I created a script with the same function in it and had it run thousands of time without experiencing the same behavior. I will try first updating to the latest version of CodeIgniter 2 and if that does not work we will have to try out the latest of version 3. I will update once I have a resolution for anyone else who comes to this problem.</p>