title: sugarCRM add document from script
body: <p>I am trying to add Document to Sugar object (client) from PHP script. I have a directory of files (on the same server where sugarCRM is installed) and xls with sugar objec ID and filename). PHP Script should add correct filename to specific sugar object (identified with ID). I can read XLS this is no problem, I can also get instance of sugar object (retrieve by ID), but I have no idea how can I assign the file to sugar. I was trying with Document, and upload_file.php, but those seem to be usable with uploading single file with html Form. </p>
  
  <p>How can I automate this task, copy files with correct filename to <code>cache\upload</code> and create Document related to my Customer from PHP Script? I would prefer not to use SOAP if it's not necesarry...</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>I was able to save document and revision, but something is wrong, and file can't be downloaded from browser ("incorrect call to file")</p>
  
  <p>My Code so far is:</p>
  
  <pre><code>  require_once('include/upload_file.php');
  
    $upload_file = new UploadFile('uploadfile');
    $document-&gt;filename = 'robots.txt';
    $document-&gt;document_name = 'robots.txt';
  
    $document-&gt;save();
    $contents = file_get_contents ($document-&gt;filename);
  
    $revision = new DocumentRevision;
    $revision-&gt;document_id = $document-&gt;id;
    $revision-&gt;file = base64_encode($contents);
    $revision-&gt;filename = $document-&gt;filename;
    $revision-&gt;revision = 1;
    $revision-&gt;doc_type = 'Sugar';
    $revision-&gt;file_mime_type = 'text/plain';
  
    $revision-&gt;save();
  
    $document-&gt;revision_id = $revision-&gt;id;
    $document-&gt;save();
  
    $destination = clean_path($upload_file-&gt;get_upload_path($document-&gt;id));
  
    $fp = sugar_fopen($destination, 'wb');
  
    if( !fwrite($fp, $contents) ){
        die("ERROR: can't save file to $destination");
    }
  
    fclose($fp);
  </code></pre>