title: Silverstripe: User-specific files module
body: <p>I've got an idea for a project and wondering if there's an already existing module that can achieve something similar. Basically I want to allow the users to upload/download files from their member profile. Users will only be able to view/download their own files. I have member profiles set up already, it's the file handling that I need to get working now.</p>
  
  <p>I imagine it will be something like a gridfield displayed on the users profile. When they use an upload field it will save files in to assets/ folder. Which can only be viewed by that member (admins can view all in the /admin section of the CMS.</p>
  
  <p>Are there any current modules that support something like this?</p>
  
  <p>If I create an extension of the Member profile that allows uploading files, how could I implement the security for only users with the currentID to view files?</p>
  
  <p>EDIT: OK So I've made some progress with this. The upload file function works. It saves it in to a folder which is named after the user. However, it saves the uploaded file in that directory, but also a blank file under the "Files" directory. Here is my code...</p>
  
  <pre><code>//User file upload function
      public function UploadUserFile() {
          $fields = new FieldList($field = new UploadField('UserFiles', 'Upload files'));
          $field-&gt;setCanAttachExisting(false); // Block access to SilverStripe assets library
          $field-&gt;setCanPreviewFolder(false); // Don't show target filesystem folder on upload field
          $field-&gt;setFolderName('user-files/user-'.Member::currentUserID()); //Upload to a user specific folder
          $actions = new FieldList(new FormAction('submit', 'Save Images'));
          $form = new Form($this, 'UploadUserFile', $fields, $actions, null);
          return $form;
      }
  
      public function submit($data, $form) {
          $file = new File();
          $form-&gt;saveInto($file);
          $file-&gt;write();
          return $this;
      }
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>Turns out that the submit function was causing the extra file. Disregard. Still experiencing other problems now but will see if I can fix it first.</p>
  
  <p>EDIT 3:</p>
  
  <p>OK, I've had a go and I am able to display files on the front end. Here is my code...</p>
  
  <pre><code>//User file upload function
      public function UploadUserFile() {
          $fields = new FieldList($field = new UploadField('UserFiles', 'Upload files'));
          $field-&gt;setCanAttachExisting(false); // Block access to SilverStripe assets library
          $field-&gt;setCanPreviewFolder(false); // Don't show target filesystem folder on upload field
          $field-&gt;setFolderName('User-Files/User-'.Member::currentUserID().'-'.Member::currentUser()-&gt;Surname.Member::currentUser()-&gt;FirstName); //Upload to a user specific folder
          $field-&gt;setAutoUpload(false);
          $actions = new FieldList(new FormAction('submit', 'Save Images'));
          $form = new Form($this, 'UploadUserFile', $fields, $actions, null);
          return $form;
      }
      //Refresh files function
      public function submit($data, $form) {
          return $this-&gt;redirectBack();
      }
      //Display users files
      public function DisplayFiles() {
          $arrayList = ArrayList::create();
          $files = File::get()-&gt;filter(array(
              "OwnerID" =&gt; Member::currentUserID(),
              "ClassName:not" =&gt; 'Folder'
          ));
          foreach($files as $file) {
  
              if($file-&gt;canView()) {
                  $arrayList-&gt;push($file);
              }
          }
          return $arrayList;
      }
  </code></pre>
  
  <p>I'm trying to allow users to delete their file from the front end. What's the best way to handle this?</p>
  
  <p>Thanks</p>