title: Insert files in MongoDBs GridFS
body: <p>I am trying to insert a bunch of pdf-files into MongoDB using GridFS, but get a <code>MongoGridFSException</code> with the message: error setting up file <em>name</em> in <em>path</em>, where <em>name</em> is the absolute path to the pdf-document on the file system.</p>
  
  <p>I'm using a very simple CLI-script:</p>
  
  <pre><code>$it = new DirectoryIterator(__DIR__ .'/files');
  while ($it as $file) {
      $grid-&gt;storeFile($file-&gt;getPathname(), array('metadata' =&gt; $metadata));
  }
  </code></pre>
  
  <p>I find the lack of documentation for GridFS disturbing. The examples in <a href="http://www.php.net/manual/en/class.mongogridfs.php" rel="nofollow">MongoGridFS</a> and <a href="http://www.php.net/manual/en/mongogridfs.storefile.php" rel="nofollow">storeFile()</a> all just show a simple filename as first argument; if the data is retrieved from this filename, what basePath does the script assume (<code>cwd</code>, <code>__DIR__</code> or something completely different?) or can I just add the absolute path (judging from the error message, probably not)?</p>
  
  <p>I was contemplating on using <code>storeBytes(file_get_contents($file-&gt;getPathname())</code> instead, but then how do I retrieve the file, when there is no filename associated with it? Do I add the filename in the 2nd argument (options-array)?</p>
  
  <p>Also, if I want to know the mine/type of a stored file, do I have to specify it in the metadata manually or is this determined automatically when inserting the file?</p>
  
  <p>TL;DR: I have no idea how to work with GridFS and the php-docs are of poor quality. Can at the very least, someone point out better examples/documentation?</p>