move() публичный Метод

Move uploaded file to new location.
public move ( $dest ) : self
Результат self
Пример #1
1
 /**
  * @ORM\PreFlush()
  */
 public function preUpload()
 {
     if ($this->file) {
         if ($this->file instanceof FileUpload) {
             $basename = $this->file->getSanitizedName();
             $basename = $this->suggestName($this->getFilePath(), $basename);
             $this->setName($basename);
         } else {
             $basename = trim(Strings::webalize($this->file->getBasename(), '.', FALSE), '.-');
             $basename = $this->suggestName(dirname($this->file->getPathname()), $basename);
             $this->setName($basename);
         }
         if ($this->_oldPath && $this->_oldPath !== $this->path) {
             @unlink($this->getFilePathBy($this->_oldProtected, $this->_oldPath));
         }
         if ($this->file instanceof FileUpload) {
             $this->file->move($this->getFilePath());
         } else {
             copy($this->file->getPathname(), $this->getFilePath());
         }
         return $this->file = NULL;
     }
     if (($this->_oldPath || $this->_oldProtected !== NULL) && ($this->_oldPath != $this->path || $this->_oldProtected != $this->protected)) {
         $oldFilePath = $this->getFilePathBy($this->_oldProtected !== NULL ? $this->_oldProtected : $this->protected, $this->_oldPath ?: $this->path);
         if (file_exists($oldFilePath)) {
             rename($oldFilePath, $this->getFilePath());
         }
     }
 }
Пример #2
0
 /**
  * @param FileUpload $file
  * @return Image
  * @throws NotImageUploadedException
  * @throws FileSizeException
  * @throws DBALException
  * @throws InvalidStateException
  */
 public function processImage(FileUpload $file)
 {
     if (!$file->isImage()) {
         throw new NotImageUploadedException();
     }
     if (\filesize($file->getTemporaryFile()) > Image::MAX_FILE_SIZE) {
         throw new FileSizeException();
     }
     try {
         $this->em->beginTransaction();
         $image = new Image($file);
         $this->em->persist($image)->flush();
         $file->move($this->composeImageLocation($image));
         $this->em->commit();
     } catch (InvalidStateException $is) {
         $this->em->rollback();
         $this->em->close();
         $this->logger->addError('Error occurs while moving temp. image file to new location.');
         throw $is;
     } catch (DBALException $e) {
         $this->em->rollback();
         $this->em->close();
         $this->logger->addError('Image error');
         // todo err message
         throw $e;
     }
     return $image;
 }
Пример #3
0
 /**
  * Process form file upload from FileUpload wrapper class.
  *
  * @param FileUpload $file
  * @return string
  */
 public static function upload(FileUpload $file)
 {
     $filename = $file->getSanitizedName();
     $folder = date('Y');
     $file->move(self::getUploadDir() . $folder . '/' . $filename);
     return $folder . '/' . $filename;
 }
Пример #4
0
 public function create(Product $product, FileUpload $fileUpload)
 {
     switch ($fileUpload->getContentType()) {
         case 'image/jpeg':
             $suffix = 'jpg';
             break;
         case 'image/png':
             $suffix = 'png';
             break;
         case 'image/gif':
             $suffix = 'gif';
             break;
         default:
             throw new EntityInvalidArgumentException(sprintf('File is of an unknown type %s.', $fileUpload->getContentType()));
     }
     $baseName = sprintf('%s-%%s.%s', Strings::webalize($product->getName()), $suffix);
     do {
         $fileName = sprintf($baseName, Random::generate(5, '0-9a-zA-Z'));
         $path = sprintf('%s/%s', $this->imagesDir, $fileName);
     } while (file_exists($path));
     $fileUpload->move($path);
     $image = new ProductImage($product, $fileName);
     $this->createEntity($image);
     $product->addImage($image);
     return $image;
 }
Пример #5
0
 /**
  * @param FileUpload $fileUpload
  */
 public function upload(FileUpload $fileUpload, $path)
 {
     if (!$fileUpload->isOk()) {
         throw new ImageUploadedException('Chyba při nahrávání obrázku');
     }
     $fileUpload->move($path);
 }
Пример #6
0
 protected function processImageUpload(Nette\Http\FileUpload $upload, $targetDir, $id)
 {
     $dir = 'upload' . DIRECTORY_SEPARATOR . $targetDir . DIRECTORY_SEPARATOR . $id;
     if (!file_exists($this->rootDir . DIRECTORY_SEPARATOR . $dir)) {
         Nette\Utils\FileSystem::createDir($this->rootDir . DIRECTORY_SEPARATOR . $dir);
     }
     $path = $dir . DIRECTORY_SEPARATOR . $upload->getSanitizedName();
     $upload->move($this->rootDir . DIRECTORY_SEPARATOR . $path);
     return $path;
 }
Пример #7
0
 /**
  * @param FileUpload $file
  * @return mixed|void
  * @throws NotAllowedFileException
  */
 public function save(FileUpload $file)
 {
     $filename = $file->getSanitizedName();
     $parts = explode('.', $filename);
     $last = end($parts);
     if (in_array($last, $this->notAllowed)) {
         throw new NotAllowedFileException("File with {$last} suffix is not allowed.");
     }
     $file->move($this->dir . '/' . $filename);
 }
Пример #8
0
 protected function saveFile(FileUpload $upload)
 {
     if ($upload->ok) {
         $pathinfo = pathinfo($upload->name);
         $extension = in_array($pathinfo['extension'], $this->disableExtension) ? 'txt' : $pathinfo['extension'];
         $file = $this->path . '/' . md5(uniqid(rand(), true)) . '.' . $extension;
         $upload->move($this->wwwDir . '/' . $file);
         return ['name' => $upload->name, 'src' => $file];
     }
     return NULL;
 }
             $values->avatar = $this->_uploadTitleImage($values->avatar);
             if (is_file("www/" . $this->avatar_path . $values->old_avatar)) {
                 unlink($this->www_dir . "/www/" . $this->avatar_path . $values->old_avatar);
             }
             $this->hlavne_menu->uloz(["avatar" => $values->avatar], $values->id);
         } else {
             throw new Database\DriverException('Pre titulný obrázok nebol použitý obrázok a tak nebol uložený!' . $e->getMessage());
         }
     } else {
         throw new Database\DriverException('Pri pokuse o uloženie došlo k chybe! Pravdepodobná príčina je:' . $this->presenter->upload_error[$values->avatar->error] . $e->getMessage());
     }
 } catch (Database\DriverException $e) {
Пример #10
0
 /**
  * @param FileUpload $file
  * @return Image
  * @throws \Nette\InvalidArgumentException
  */
 public function upload(FileUpload $file)
 {
     if (!$file->isOk() || !$file->isImage()) {
         throw new Nette\InvalidArgumentException();
     }
     do {
         $name = Random::generate() . '.' . $file->getSanitizedName();
     } while (file_exists($path = $this->imagesDir . "/" . $this->namespace . $this->originalPrefix . "/" . $name));
     $file->move($path);
     $this->onUploadImage($path, $this->namespace);
     $this->namespace = NULL;
     return new Image($path);
 }
Пример #11
0
 public function savaOriginImage($namespace, FileUpload $file)
 {
     if (!$file->isOk() || !$file->isImage()) {
         throw new ImageStorageException("invalid image");
     }
     $i = 1;
     $name = $file->getSanitizedName();
     $path = $this->getPath($namespace, $name);
     while (file_exists($path)) {
         $name = $i . '-' . $file->getSanitizedName();
         $path = $this->getPath($namespace, $name);
         $i++;
     }
     $file->move($path);
     return $name;
 }
Пример #12
0
 /**
  * @param  FileUpload
  * @param  string|NULL
  * @param  array|string|NULL
  * @return string  filepath (namespace/file.ext)
  * @throws ImageStorageException
  */
 public function upload(FileUpload $image, $namespace = NULL, $mimeTypes = NULL)
 {
     if (!$image->isOk()) {
         throw new ImageStorageException('File is broken');
     }
     if (!$image->isImage()) {
         $contentType = $image->getContentType();
         $isValid = FALSE;
         if (isset($mimeTypes)) {
             $mimeTypes = is_array($mimeTypes) ? $mimeTypes : explode(',', $mimeTypes);
             $isValid = in_array($contentType, $mimeTypes, TRUE);
         }
         if (!$isValid) {
             throw new ImageStorageException('File must be image, ' . $contentType . ' given');
         }
     }
     $sanitizedName = $image->getSanitizedName();
     $ext = pathinfo($sanitizedName, PATHINFO_EXTENSION);
     $sanitizedName = pathinfo($sanitizedName, PATHINFO_FILENAME) . '.' . Strings::lower($ext);
     $file = $this->generateFilePath($sanitizedName, $namespace);
     $path = $this->getPath($file);
     $image->move($path);
     return $file;
 }
Пример #13
0
 /**
  * Save uploaded files.
  *
  * @param string $lang
  * @param FileUpload $po
  * @param FileUpload $mo
  */
 public function upload($lang, FileUpload $po, FileUpload $mo)
 {
     $mo->move($this->getFile($lang, 'mo'));
     $po->move($this->getFile($lang, 'po'));
 }
Пример #14
0
 /**
  * Adds file to queue
  * @param FileUpload $file
  */
 function addFile(FileUpload $file)
 {
     $file->move($this->getUniqueFilePath());
     $data = array('queueID%s' => $this->getQueueID(), 'created%i' => time(), 'data%s' => base64_encode(serialize($file)), 'name%s' => $file->getName());
     $this->query('INSERT INTO [files]', $data);
 }
Пример #15
0
 /**
  * @internal
  *
  * @ORM\PreFlush()
  */
 public function preFlush()
 {
     if ($this->removed) {
         return;
     }
     if ($this->file) {
         if ($this->file instanceof FileUpload) {
             $basename = $this->file->getSanitizedName();
             $basename = $this->suggestName($basename);
             $this->setName($basename);
         } else {
             $basename = trim(Strings::webalize($this->file->getBasename(), '.', false), '.-');
             $basename = $this->suggestName($basename);
             $this->setName($basename);
         }
         if ($this->oldPath && $this->oldPath !== $this->path) {
             $filePath = $this->getFilePathBy($this->oldProtected, $this->oldPath);
             if (!is_file($filePath)) {
                 throw new RemoveFileException(sprintf('File \'%s\' does not exist.', $filePath));
             }
             unlink($filePath);
             if (is_file($filePath)) {
                 throw new RemoveFileException(sprintf('File \'%s\' cannot be removed. Check access rights.', $filePath));
             }
         }
         if ($this->file instanceof FileUpload) {
             try {
                 $filePath = $this->getFilePath();
                 $this->file->move($filePath);
             } catch (\Nette\InvalidStateException $e) {
                 throw new UploadFileException();
             }
             if (!is_file($filePath)) {
                 throw new RemoveFileException(sprintf('File \'%s\' does not exist.', $filePath));
             }
         } else {
             $oldFilePath = $this->file->getPathname();
             $filePath = $this->getFilePath();
             if (!is_file($oldFilePath)) {
                 throw new RenameFileException(sprintf('File \'%s\' does not exist.', $oldFilePath));
             }
             if (is_file($filePath)) {
                 throw new RenameFileException(sprintf('File \'%s\' already exists.', $filePath));
             }
             copy($oldFilePath, $filePath);
             if (!is_file($filePath)) {
                 throw new RenameFileException(sprintf('File \'%s\' does not exist.', $filePath));
             }
         }
         $this->size = filesize($this->getFilePath());
         $this->mimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->getFilePath());
         return $this->file = null;
     }
     if (($this->oldPath || $this->oldProtected !== null) && ($this->oldPath !== $this->path || $this->oldProtected !== $this->protected)) {
         $oldFilePath = $this->getFilePathBy($this->oldProtected !== null ? $this->oldProtected : $this->protected, $this->oldPath !== null ? $this->oldPath : $this->path);
         $filePath = $this->getFilePath();
         if (!is_file($oldFilePath)) {
             throw new RenameFileException(sprintf('File \'%s\' does not exist.', $oldFilePath));
         }
         if (is_file($filePath)) {
             throw new RenameFileException(sprintf('File \'%s\' already exists.', $filePath));
         }
         rename($oldFilePath, $filePath);
         if (is_file($oldFilePath)) {
             throw new RenameFileException(sprintf('File \'%s\' already exists.', $oldFilePath));
         }
         if (!is_file($filePath)) {
             throw new RenameFileException(sprintf('File \'%s\' does not exist.', $filePath));
         }
     }
 }
Пример #16
0
 /**
  * Take a FileUpload, save it and return new Image
  * @param  Nette\Http\FileUpload $upload
  * @param  string                $namespace
  * @param  string                $checksum
  * @return Image
  */
 public function saveUpload(Nette\Http\FileUpload $upload, $namespace, $checksum = NULL)
 {
     if (!$checksum) {
         $checksum = call_user_func_array($this->algorithm_file, [$upload->getTemporaryFile()]);
     }
     list($path, $identifier) = $this->getSavePath(self::fixName($upload->getName()), $namespace, $checksum);
     $upload->move($path);
     $image = new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, ['sha' => $checksum, 'name' => self::fixName($upload->getName())]);
     return $image;
 }
Пример #17
0
 /**
  * @param \Nette\Http\FileUpload $file
  * @param string $filename
  * @throws IOException
  * @return string
  */
 public function writeUploaded(Nette\Http\FileUpload $file, $filename = NULL)
 {
     if (!$file->isOk()) {
         throw new IOException("Cannot save corrupted file.");
     }
     do {
         $name = Nette\Utils\Strings::random(10) . '.' . ($filename ?: $file->getSanitizedName());
     } while (file_exists($path = $this->dir . DIRECTORY_SEPARATOR . $name));
     $file->move($path);
     return basename($path);
 }
Пример #18
0
 /**
  * Moves a uploaded document file to storage directory.
  * @param  object $file object of Nette\Http\FileUpload
  * @return void
  */
 public function uploadDocFile(FileUpload $file)
 {
     $docdir = $this->presenter->docDir;
     if (!$this->document) {
         throw new DirectiveException(DirectiveException::DOCUMENT_NOT_SET);
     }
     $newFile = $docdir . "/" . $this->document;
     $file->move($newFile);
 }
Пример #19
0
 /**
  * save file and return path
  *
  * @param Nette\Http\FileUpload $image
  * @return string
  * @throws \Nette\Utils\UnknownImageFileException
  */
 public function saveImage(Nette\Http\FileUpload $image)
 {
     $filename = $image->getSanitizedName();
     $path = $this->dir;
     $filename = $this->getFileName($path, $filename);
     $image->move("{$path}/{$filename}");
     $filePath = "{$this->uri}/{$filename}";
     $image = Image::fromFile("{$path}/{$filename}");
     $image->resize(150, 150, Image::EXACT);
     $image->save("{$path}/{$filename}");
     return $filePath;
 }
Пример #20
0
 /**
  * @param Nette\Http\FileUpload $file
  * @return string|NULL
  */
 public function saveUpload(Nette\Http\FileUpload $file)
 {
     if ($file->isOk()) {
         $fileName = $this->getFileNameForSave($file->getName());
         $origPath = $this->getOrigPath($fileName);
         $file->move($origPath);
         if ($this->createImage($origPath, $this->getCompressionPath($fileName))) {
             return basename($origPath);
         }
     }
     return NULL;
 }
Пример #21
0
 /**
  * Save uploaded file
  *
  * @param FileUpload $file
  * @param string $path
  * @return string
  */
 public static function save(FileUpload $file, $path)
 {
     if (!$file->isOk()) {
         return FALSE;
     }
     $pathName = self::prepareToSaveFile($file->getSanitizedName(), $path);
     $file->move($pathName);
     return basename($pathName);
 }
Пример #22
0
 public function put(FileUpload $file)
 {
     if (!$file->ok) {
         $this->errors[] = "Při nahrávání obrázku " . $file->name . " nastala chyba " . $file->error;
         return false;
     }
     // Allow certain file formats
     $fileType = $this->verifyFileType(basename($file->name));
     if (!$fileType) {
         $this->errors[] = "Nahraný soubor " . $file->name . " není jedním z povolených typů: " . implode(", ", self::allowedFileTypes());
         return false;
     }
     // Check if image file is a actual image or fake image
     $dimensionErrors = self::checkImageDimensions($file->temporaryFile);
     if (!empty($dimensionErrors)) {
         $this->errors = $dimensionErrors;
         return false;
     }
     // Check file size
     if (($file_size = $file->size) > self::MAX_IMG_FILE_SIZE) {
         $errors[] = "Velikost souboru " . $file_size / 1024 . "kb přesáhla povolený limit " . self::MAX_IMG_FILE_SIZE / 1024 . "kb";
         return false;
     }
     // if everything is ok, try to upload file
     $destFile = $this->uniqueFilename($file->name);
     $finalFileName = $this->imgFolder . $destFile;
     if (!$file->move($finalFileName)) {
         $errors[] = "Nahraný obrázek se nepodařilo uložit.";
         return false;
     }
     return $destFile;
 }
Пример #23
0
 /**
  * Add file to queue
  * @param \Nette\Http\FileUpload $file
  */
 function addFile(\Nette\Http\FileUpload $file)
 {
     $file->move($this->getUniqueFilePath());
     $this->query("\n                INSERT INTO files (queueID, created, data, name) VALUES (\n                '" . Sqlite3::escapeString($this->getQueueID()) . "'," . time() . "," . "'" . Sqlite3::escapeString(serialize($file)) . "'," . "'" . Sqlite3::escapeString($file->getName()) . "')\n            ");
 }
Пример #24
0
 /**
  * Prida soubor do db
  * @param \Nette\Http\FileUpload $file
  * @return type 
  */
 public function uploadFile($file, $name)
 {
     return $file->move($this->getFullPath(TRUE) . $name);
 }
Пример #25
0
 public function move($dest)
 {
     $source = $this->getTemporaryFile();
     $result = parent::move($dest);
     @unlink($source);
     $this->moved = true;
     return $result;
 }
Пример #26
0
 /**
  * Adds file to queue
  * @param FileUpload $file
  */
 function addFile(FileUpload $file)
 {
     $file->move($this->getUniqueFilePath());
     $data = array('queueID' => $this->getQueueID(), 'created' => time(), 'data' => base64_encode(serialize($file)), 'name' => $file->getName());
     $this->getConnection()->table($this->getFilesTable())->insert($data);
 }
Пример #27
0
 /**
  * Adds file to queue
  * @param FileUpload $file
  */
 function addFile(FileUpload $file)
 {
     $file->move($this->getUniqueFilePath());
     $this->query('INSERT INTO files (queueID, created, data, name) VALUES ("' . sqlite_escape_string($this->getQueueID()) . '",' . time() . ',\'' . sqlite_escape_string(serialize($file)) . '\', \'' . sqlite_escape_string($file->getName()) . '\')');
 }
Пример #28
0
 /**
  * @param \Nette\Http\FileUpload $fileUpload
  * @return string
  */
 protected function upload(FileUpload $fileUpload)
 {
     $fileName = $fileUpload->getSanitizedName();
     while (file_exists($this->uploadPath . '/' . $fileName)) {
         $fileName = Random::generate() . $fileUpload->getSanitizedName();
     }
     $uploadPath = $this->uploadPath . '/' . $fileName;
     $fileUpload->move($uploadPath);
     return $uploadPath;
 }
Пример #29
0
 /**
  * File upload handler. Saves file into upload folder and saves origin name to cache.
  *
  * @return void
  */
 public function handlePluploadFile()
 {
     // $this->getParameter('token') is not working
     if (!array_key_exists($this->getParameterId('token'), $_GET)) {
         $this->presenter->sendJson(array('status' => 'error', 'message' => 'Token is missing.'));
     }
     $token = $_GET[$this->getParameterId('token')];
     if (empty($_FILES)) {
         $this->presenter->sendJson(array('File is missing.'));
     }
     $file = new Nette\Http\FileUpload(end($_FILES));
     if (!$file->isOk()) {
         $this->presenter->sendJson(array('status' => 'error', 'message' => $this->getError($file->getError())));
     }
     $tempName = uniqid('np-') . '.' . pathinfo($file->getName(), PATHINFO_EXTENSION);
     $file->move($this->form->getUploadDir() . '/' . $tempName);
     $files = $this->form->cache->load($token, function () {
         return array();
     });
     $files[] = $file;
     $this->form->cache->save($token, $files);
     $this->presenter->sendJson(array('status' => 'success'));
 }
Пример #30
0
 /**
  *
  * @param \Nette\Http\FileUpload $file
  * @param string $path
  * @return TRUE
  */
 public static function save(\Nette\Http\FileUpload $file, $path)
 {
     if (!$file->isOk()) {
         return FALSE;
     }
     self::mkDir($path);
     $path = realpath($path) . DIRECTORY_SEPARATOR;
     $name = $file->sanitizedName;
     do {
         $pathName = $path . $name;
         $name = \Nette\Utils\Strings::random(5) . $name;
     } while (file_exists($pathName));
     $file->move($pathName);
     return basename($pathName);
 }