public static function rename($file, $location, $prefix, $name = null) { $extension = pathinfo($file["name"], PATHINFO_EXTENSION); $newName = $name ? $name : uniqid($prefix . '-') . rand(1000000, 9999999) . "." . $extension; $loc = ROOT_PATH . '/public/' . $location . '/' . $newName; try { $rename = new RenameUpload(array('target' => $loc, 'overwrite' => true)); $filter = $rename->filter($file); $mode = 644; if (!$filter || !chmod($loc, octdec($mode))) { return false; } return $newName; } catch (\Exception $e) { return false; } }
/** * {@inheritdoc} */ public function filter($value) { $return = parent::filter($value); $imagePath = isset($return['tmp_name']) ? $return['tmp_name'] : $return; $watermarkOptions = $this->getWatermark(); $watermark = new Watermark(); if (!$watermarkOptions['image'] instanceof Watermark\Adapter\AdapterInterface) { $watermarkOptions['image'] = new $watermarkOptions['adapter']($watermarkOptions['image']); } $watermark->setWatermark($watermarkOptions['image']); $watermark->setPosition($watermarkOptions['position']); $watermark->setMargins($watermarkOptions['margins']); if (!$imagePath instanceof Watermark\Adapter\AdapterInterface) { $imagePath = new $watermarkOptions['adapter']($imagePath); } $watermark->filter($imagePath); return $return; }
/** * {@inheritDoc} */ public function storeImage(UserInterface $user, array $files) { $form = $this->getServiceLocator()->get('HtProfileImage\\ProfileImageForm'); $this->getEventManager()->trigger(__FUNCTION__, $this, ['files' => $files, 'form' => $form, 'user' => $user]); $inputFilter = new ProfileImageInputFilter($this->getOptions()->getUploadDirectory(), $user, $this->getOptions()->getMaxImageFileSize()); $inputFilter->init(); $form->setInputFilter($inputFilter); $form->setData($files); if ($form->isValid()) { $uploadTarget = $inputFilter->getUploadTarget(); $filter = new RenameUpload($uploadTarget); $filter->setOverwrite(true); $filter->filter($files['image']); $newFileName = $this->getStorageModel()->getUserImage($user); $filterAlias = $this->getOptions()->getStorageFilter(); if (!$filterAlias) { rename($uploadTarget, $newFileName); //no filter alias given, just rename } else { $filter = $this->getFilterManager()->getFilter($filterAlias); $image = $this->getImagine()->open($uploadTarget); try { $image = $filter->apply($image); // resize the image } catch (\Exception $e) { return false; } $image->save($newFileName); // store the image } unlink($uploadTarget); $this->deleteCache($user); $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, ['image_path' => $newFileName, 'user' => $user]); return true; } return false; }
/** * Saves uploaded file to server dump folder with filename * LISBACKUP_upload_DDMMYYYY_HHMMSS.sql * TODO: Sanity checking, that the input file is actually an SQL dump. * * @param array $file */ public function upload($file) { try { $this->setFileName('upload'); $filter = new \Zend\Filter\File\RenameUpload(_PATH_ . $this->fileName); $filter->filter($file); if ($filter->filter($file)['error'] == 4) { return array('error' => true, 'exception' => "You must select a file for upload."); } else { var_dump($filter->filter($file)); return array('error' => false); } } catch (Exception $ex) { return array('error' => true, 'exception' => 'This Error happened:<br>' . $ex); } }
/** * * @param array $fileInfo * @param string $targetPath * OPTIONAL target directory (realpath) * @param boolean $overwrite * OPTIONAL whether or not to replace existing * @return array */ public function createFile(array $fileInfo, $targetPath = null, $overwrite = false) { $it = $this->getStorage(); if (empty($targetPath) || !is_dir($targetPath)) { $targetPath = $it->getRealPath(); } $filter = new RenameUpload($targetPath); $filter->setOverwrite($overwrite); $filter->setUseUploadName(true); return $filter->filter($fileInfo); }
public function attach(AttachmentFieldsetProvider $form, $type = 'file', $appendId = null) { if (!$form->isValid()) { throw new Exception\RuntimeException(print_r($form->getMessages(), true)); } if (!$appendId) { $this->assertGranted('attachment.create'); $attachment = $this->createAttachment(); $type = $this->getTypeManager()->findTypeByName($type); $attachment->setType($type); } else { $attachment = $this->getAttachment($appendId); $this->assertGranted('attachment.append', $attachment); } $data = $form->getData()['attachment']; if (!isset($data['file']) || $data['file']['error']) { throw new Exception\NoFileSent(); } $file = $data['file']; $filename = $file['name']; $size = $file['size']; $filetype = $file['type']; $pathinfo = pathinfo($filename); $extension = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : ''; $hash = uniqid() . '_' . hash('ripemd160', $filename) . $extension; $location = $this->findParentPath($this->moduleOptions->getPath()) . '/' . $hash; $webLocation = $this->moduleOptions->getWebpath() . '/' . $hash; $filter = new RenameUpload($location); $filter->filter($file); return $this->attachFile($attachment, $filename, $webLocation, $size, $filetype); }
/** * @param array $fileData * @return static * @throws \InvalidArgumentException */ public static function upload(array $fileData) { if (!is_array($fileData)) { throw new \InvalidArgumentException('Invalid fileData for upload'); } if (!isset($fileData['name']) && count($fileData) == 1) { return self::upload(array_pop($fileData)); } $renameUpload = new RenameUpload(self::getDefaultUploadBasePath() . DIRECTORY_SEPARATOR . $fileData['name']); $renameUpload->filter($fileData); return new static($fileData['name']); }
/** * Save resource * * @access public * @param Courses\Entity\Resource $resource * @param array $data ,default is empty array * @param bool $isAdminUser ,default is bool false * @param string $userEmail ,default is null */ public function save($resource, $data = array(), $isAdminUser = false, $userEmail = null) { $notifyAdminFlag = false; $editFlag = false; if (empty($data)) { $editFlag = true; } if ($isAdminUser === false) { $resource->setStatus(Status::STATUS_NOT_APPROVED); $notifyAdminFlag = true; } $this->query->setEntity('Courses\\Entity\\Resource')->save($resource, $data); if (isset($data["fileAdded"])) { // save added resources foreach ($data["fileAdded"] as $fileKey => $fileValue) { $filter = new RenameUpload($fileValue["uploadOptions"]); $resource = clone $resource; $uploadedFile = $filter->filter($fileValue); $resource->setFile($uploadedFile); $resource->setName($data["nameAdded"][$fileKey]); $this->query->setEntity('Courses\\Entity\\Resource')->save($resource); } } if ($notifyAdminFlag === true) { $this->sendMail($userEmail, $editFlag); } }