Ejemplo n.º 1
0
 public function upload(DownloadSession $downloadSession)
 {
     // the file property can be empty if the field is not required
     if (null === $this->getFile()) {
         return false;
     }
     // use the original file name here but you should
     // sanitize it at least to avoid any security issues
     $path = $downloadSession->getId();
     if (!is_dir($this->getUploadRootDir() . '/' . $path)) {
         if (!mkdir($this->getUploadRootDir() . '/' . $path)) {
             throw new \Exception('Create dir exception');
         }
     }
     $filename = sha1(uniqid(mt_rand(), true)) . '.' . $this->getFile()->guessExtension();
     // move takes the target directory and then the
     // target filename to move to
     $this->getFile()->move($this->getUploadRootDir() . '/' . $path, $filename);
     // set the path property to the filename where you've saved the file
     $this->path = $path . '/' . $filename;
     // clean up the file property as you won't need it anymore
     $this->file = null;
     return true;
 }