title: PHP: Copy file to php-created-directory in Ubuntu didn't work
body: <p>I have an ubuntu server. My objective is to create a directory within the root directory for the ftp user (suresh, say) copy some files from the latter to the newly created directory.</p>
  
  <p><strong>The problem is, I am able to create new directory, but when I try to copy files within this directory, it wont work.</strong></p>
  
  <p>I tested the script to copy file locally (in the root directory), it works. I changed the permission of the newly created directory to 0777, it works, but not for 0775. I tried two examples of copy. Both work.</p>
  
  <p>The owner user/group of the newly created directory is www-data (its a ubuntu server with root access). The permission is 755 for the directory. Changing the owner to the ftp user suresh didn't make it either.</p>
  
  <p>The following is the php I used:</p>
  
  <pre><code>&lt;?php
  //To create directory/subdirectory recursively
  
  $mode = 0775; 
  mkdir("./directory/subdirectory", $mode, TRUE);
  
  //Source and destination
  $source = "index.php";
  $dest = "directory/index.php"
  
  //Copy function first version
  function stream_copy($src, $dest) 
      {
          $fsrc = fopen($src,'r');
          $fdest = fopen($dest,'w+');
          $len = stream_copy_to_stream($fsrc,$fdest); 
          fclose($fsrc);
          fclose($fdest);
          return $len; 
  } 
  
  if (!stream_copy($source,$dest)){
      echo "File stream copy to directory not successful! &lt;br /&gt;";
  } else {
      echo "Check the directory for index file! &lt;br /&gt;";
  }
  
  //And I always get the "...not successful" output!
  
  //Copy function second version
  function copyemz($file1,$file2){
      $contentx =@file_get_contents($file1); 
      $openedfile = fopen($file2, "w"); 
      fwrite($openedfile, $contentx); 
      fclose($openedfile); 
      if ($contentx === FALSE) { 
      $status=false; 
  } else $status=true;
  
      return $status; 
  }
  
  if (!copyemz($source,$dest)){
      echo "File copy to directory not successful &lt;br /&gt;";
  } else {
      echo "Check the directory for index!&lt;br /&gt;";
  }
  
  //The same result for this either!
  
  ?&gt;
  </code></pre>
  
  <p>Finally I tried the shell_exec and same result for this as well! All these work with the conditions set a</p>
  
  <p>Can anyone explain me what is happening or what I am missing!</p>
  
  <p>The user for ftp (suresh) is a group-member of www-data, and vice-versa. (Just if it helps! Tried otherwise also.)</p>