title: How to store file and retrive it correctly with moodle file api?
body: <p>i'm programming a mod activity for moodle which load files and show'em to any student who can access to the course.</p>
  
  <p>The problem is that handing files in moodle is damn hard.</p>
  
  <p>this is what i have done so far:</p>
  
  <p>option page with importers</p>
  
  <pre><code>$mform-&gt;addElement('filepicker', 'slidesyncmedia', get_string('slidesyncmedia', 'slidesync'), null, array('maxbytes' =&gt; $maxbytes, 'accepted_types' =&gt; '*'));
  
  $mform-&gt;addElement('filemanager', 'slidesyncslides', get_string('slidesyncslides', 'slidesync'), null, array('subdirs' =&gt; 0, 'maxbytes' =&gt; $maxbytes, 'maxfiles' =&gt; 50, 'accepted_types' =&gt; array('*') ));
  </code></pre>
  
  <p>after submit the files are stored in draft
  and everything is loaded in another page that save all on db</p>
  
  <pre><code>if ($draftitemid = file_get_submitted_draft_itemid('slidesyncmedia')) {
          file_save_draft_area_files($draftitemid, $context-&gt;id, 'mod_slidesync', 'slidesyncmedia', 0, array('subdirs' =&gt; 0, 'maxfiles' =&gt; 1));
      }
      if ($draftitemid = file_get_submitted_draft_itemid('slidesyncslides')) {
          file_save_draft_area_files($draftitemid, $context-&gt;id, 'mod_slidesync', 'slidesyncslides', 0, array('subdirs' =&gt; 0, 'maxfiles' =&gt; 50));
      }
  </code></pre>
  
  <p>in the end i use again the first page in another place (if files are there, then shows them)</p>
  
  <pre><code>            $fs = get_file_storage();
          if ($files = $fs-&gt;get_area_files($context-&gt;id, 'mod_slidesync', 'slidesyncslides', '0', 'sortorder', false)) {
              // Look through each file being managed
              foreach ($files as $file) {
              // Build the File URL. Long process! But extremely accurate.
              $fileurl = moodle_url::make_pluginfile_url($file-&gt;get_contextid(), $file-&gt;get_component(), $file-&gt;get_filearea(), $file-&gt;get_itemid(), $file-&gt;get_filepath(), $file-&gt;get_filename());
              echo $fileurl;
              }
          } else {
              echo '&lt;p&gt;Please upload an image first&lt;/p&gt;';
          }
  </code></pre>
  
  <p>this make an url but if clicked moodle says that file does not exist
  mysite.com/pluginfile.php/53/mod_slidesync/slidesyncslides/0/Koala.jpg</p>
  
  <p>in the db the file are correctly saved!!!
      53  mod_slidesync   slidesyncslides     0   /   Koala.jpg</p>
  
  <p>what i'm missing?</p>
  
  <p>thanks</p>