Ejemplo n.º 1
0
 public function upload()
 {
     if (null === $this->file) {
         return;
     }
     $this->file->move($this->getUploadRootDir(), $this->path);
     unset($this->file);
 }
Ejemplo n.º 2
0
 public function moveUploadedFile(UploadedFile $file, $uploadBasePath)
 {
     $originalName = $file->getOriginalName();
     // use filemtime() to have a more determenistic way to determine the subpath, otherwise its hard to test.
     $relativePath = date('Y-m', filemtime($this->file->getPath()));
     $targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName;
     $targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName;
     $ext = $this->file->getExtension();
     $i = 1;
     while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) {
         if ($ext) {
             $prev = $i == 1 ? "" : $i;
             $targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath);
         } else {
             $targetFilePath = $targetFilePath . $i++;
         }
     }
     $targetDir = $uploadBasePath . DIRECTORY_SEPARATOR . $relativePath;
     if (!is_dir($targetDir)) {
         $ret = mkdir($targetDir, umask(), true);
         if (!$ret) {
             throw new \RuntimeException("Could not create target directory to move temporary file into.");
         }
     }
     $file->move($targetDir, basename($targetFilePath));
     return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath);
 }
Ejemplo n.º 3
0
 /**
  * @ORM\PostPersist()
  * @ORM\PostUpdate()
  */
 public function upload()
 {
     if (null === $this->file) {
         return;
     }
     // if there is an error when moving the file, an exception will
     // be automatically thrown by move(). This will properly prevent
     // the entity from being persisted to the database on error
     // moving file into /data
     $this->file->move($this->getUploadRootDir(), $this->path);
     // creating symlink to acces file from web/...
     symlink($this->getUploadRootDir() . '/' . $this->path, $this->getWebPath());
     unset($this->file);
 }
Ejemplo n.º 4
0
 protected function saveFile(UploadedFile $file)
 {
     $upload_dir = public_path('uploads/');
     $file_name = 'file-' . date('dmY-His') . '.' . $file->getClientOriginalExtension();
     try {
         if ($file->move($upload_dir, $file_name)) {
             return $file_name;
         } else {
             return false;
         }
     } catch (\Exception $ex) {
         return abort(500, $ex->getMessage());
     }
 }
Ejemplo n.º 5
0
 /**
  * @param UploadedFile $file
  *
  * @param null $filename
  * @return null|string
  */
 private function upload(UploadedFile $file, $filename = null)
 {
     if ($file && $file->isValid()) {
         if (!Storage::exists('page_image')) {
             Storage::makeDirectory('page_image');
         }
         if ($filename === null) {
             $filename = $this->getUniqueName('page_image', $file->getClientOriginalExtension());
         }
         $file->move('page_image', $filename);
         return $filename;
     }
     return null;
 }