title: How to do file versioning when uploading files to Alfresco using CMIS PHP
body: <p>I'm using the <a href="https://chemistry.apache.org/php/phpclient.html" rel="nofollow">Apache Chemistry CMIS PHP client</a> to upload documents from a local folder to Alfresco Community Edition 5.1 via ATOM. Here's the script I'm using to do so:</p>
  
  <pre><code>require_once ('cmis_repository_wrapper.php');
  require_once ('cmis_service.php');
  
  $repo_url = "http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom";
  $repo_username = "user";
  $repo_password = "pass";
  
  $client = new CMISService($repo_url, $repo_username, $repo_password);
  
  $repo_folder = "/alfrescoFolder";
  $source_folder = "localFolder/";
  $source_files = array_diff(scandir("$source_folder", 1), array('..', '.'));
  $myfolder = $client-&gt;getObjectByPath($repo_folder);
  
  foreach($source_files as $file)
  {
      try
      {
          $upload = $client-&gt;createDocumentFromSource($myfolder-&gt;id, $file, "$source_folder/$file");
      }
      catch(Exception $e)
      {
          echo "Some error here.";
      }
  }
  </code></pre>
  
  <p>This script works fine and documents are uploaded without problem, provided that the document doesn't already exist in the Alfresco repository. For example, let's say I have a document in my Alfresco repository named <code>example.txt</code>, and so, if I try to upload a document from my local folder with the same name, I get a CMIS constraint exception. I dont't know how I can upload a new version of an existing document.</p>
  
  <p>This is what I've tried so far, but it doesn't work:</p>
  
  <pre><code>$objs = $client-&gt;getChildren($myfolder-&gt;id);
  
  foreach($source_files as $file)
  {
      foreach($objs-&gt;objectList as $obj)
      {
          if($obj-&gt;properties['cmis:name'] == $file)
          {
              try
              {
                  $checkedout = $client-&gt;checkOut($obj-&gt;id);
                  $client-&gt;checkIn($checkedout-&gt;id);
              }
              catch(Exception $e)
              {
                  echo "Some error here.";
              }
          }
          else
          {
              try
              {
                  $upload = $client-&gt;createDocumentFromSource($myfolder-&gt;id, $file, "$source_folder/$file", array('cmis:objectTypeId'=&gt;'D:ex:document'));
              }
              catch(Exception $e)
              {
                  echo "Some error here";
              }
          }
      }
  }
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>DEBUG: postEntry: myURL = http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom/checkedoutDEBUG: postEntry: entry_template = {title} {SUMMARY} {CONTENT} {PROPERTIES} DEBUG: postEntry: properties_xml = b549c715-9a9d-427c-bd4b-c6ea29d222cb;1.0 DEBUG: postEntry: hash_values = Array ( [PROPERTIES] =&gt; b549c715-9a9d-427c-bd4b-c6ea29d222cb;1.0 [SUMMARY] =&gt; {summary} ) DEBUG: postEntry: post_value = b549c715-9a9d-427c-bd4b-c6ea29d222cb;1.0
  </code></pre>
  
  <p>What's funny is that the document is in fact locked for editing, so I don't really know what's going on. I also don't know if checking out and then checking in a document is how I'm supposed to version a document.</p>
  
  <p><strong>TL;DR</strong></p>
  
  <p>I want to be able to specify that the document I'm uploading is a new version of an existing document. Does anyone know how I can do that?</p>