/** * @param AssetInterface $asset * @param UploadedFile $file * * @return AssetVersion */ public function createVersionFromFile(AssetInterface $asset, UploadedFile $file) { list($width, $height) = getimagesize($file->getRealPath()); $extension = File::extension($file->getClientOriginalName(), $file->getMimetype()); $version = $this->version->create(['asset_id' => $asset->getId(), 'extension' => $extension, 'filesize' => $file->getClientSize(), 'filename' => $file->getClientOriginalName(), 'width' => $width, 'height' => $height, 'edited_at' => time(), 'edited_by' => Auth::getPerson()->getId(), 'mimetype' => $file->getMimeType(), 'metadata' => File::exif($file->getRealPath())]); $file->move($asset->directory(), $version->id); return $version; }
/** * Handles an uploaded file by putting it in the $targetDir. * * @param UploadedFile $uploadedFile File object that has been uploaded - usually taken from Request object. * @param string $targetDir [optional] Where to (relatively to the storage root dir) put the file? * @param array $allowed [optional] What files are allowed? If not matching then will throw exception. * @param int $maxFileSize [optional] What is the maximum allowed file size for this file? * @return File */ public function handleUploadedFile(UploadedFile $uploadedFile, $targetDir = '/', array $allowed = array(), $maxFileSize = 0) { array_walk($allowed, function ($ext) { return strtolower($ext); }); $targetDir = trim($targetDir, '/'); $targetDir = $this->path . $targetDir . (empty($targetDir) ? '' : '/'); $filenameElements = explode('.', $uploadedFile->getClientOriginalName()); $extension = array_pop($filenameElements); $extension = strtolower($extension); $filename = implode('.', $filenameElements); $targetName = StringUtils::fileNameFriendly($filename . '-' . StringUtils::random() . '.' . $extension); $targetPath = $targetDir . $targetName; // create unique file name while (file_exists($targetPath)) { $targetName = StringUtils::fileNameFriendly($filename . '-' . StringUtils::random() . '.' . $extension); $targetPath = $targetDir . $targetName; } // basic check for allowed type if (!empty($allowed) && !in_array($extension, $allowed)) { throw new FileException('The uploaded file is not of a valid type (allowed: ' . implode(', ', $allowed) . ').'); } // basic check for max allowed size if ($maxFileSize && $uploadedFile->getSize() > $maxFileSize) { throw new FileException('The uploaded file is too big (max allowed size is ' . StringUtils::bytesToString($maxFileSize) . ').'); } try { $movedFile = $uploadedFile->move(rtrim($targetDir, '/'), $targetName); } catch (SfFileException $e) { // if exception thrown then convert it to our exception throw new FileException($e->getMessage(), $e->getCode()); } $file = $this->convertSfFileToStorageFile($movedFile); return $file; }
/** * Orientate the image. * * @param UploadedFile $file * @param $orientation * @return UploadedFile */ protected function orientate(UploadedFile $file, $orientation) { $image = imagecreatefromjpeg($file->getRealPath()); switch ($orientation) { case 2: imageflip($image, IMG_FLIP_HORIZONTAL); break; case 3: $image = imagerotate($image, 180, 0); break; case 4: $image = imagerotate($image, 180, 0); imageflip($image, IMG_FLIP_HORIZONTAL); break; case 5: $image = imagerotate($image, -90, 0); imageflip($image, IMG_FLIP_HORIZONTAL); break; case 6: $image = imagerotate($image, -90, 0); break; case 7: $image = imagerotate($image, 90, 0); imageflip($image, IMG_FLIP_HORIZONTAL); break; case 8: $image = imagerotate($image, 90, 0); break; } imagejpeg($image, $file->getRealPath(), 90); return new UploadedFile($file->getRealPath(), $file->getClientOriginalName(), $file->getMimeType(), $file->getSize()); }
/** * {@inheritdoc} * * @param null|\Symfony\Component\HttpFoundation\File\UploadedFile $data */ public function convertFieldValueFromForm($data) { if ($data === null) { return null; } return array("inputUri" => $data->getRealPath(), "fileName" => $data->getClientOriginalName(), "fileSize" => $data->getSize()); }
/** * @param UploadedFile|null $uploadedFile * @param array $options * @return AssetFile */ public function handleUpload(UploadedFile $uploadedFile = null, array $options = []) { $resolver = new OptionsResolver(); $resolver->setDefaults(['type' => null, 'fallbackType' => null, 'targetUri' => null])->setAllowedTypes(['type' => ['string', 'null'], 'fallbackType' => ['int', 'null'], 'targetUri' => ['string', 'null']])->setAllowedValues(['type' => ['image', 'audio', 'file', null]]); $options = $resolver->resolve($options); if (!$uploadedFile instanceof UploadedFile || !$uploadedFile->isValid() || !($assetFile = new AssetFile($uploadedFile, null, $options['fallbackType'])) || $assetFile->getType() === null) { throw new \RuntimeException('Invalid uploaded file'); } $assetFile->setOriginalName($uploadedFile->getClientOriginalName()); if ($options['type'] !== null) { $this->validateAssetFileType($assetFile, $options['type']); } if ($options['targetUri'] !== null) { $uploadsDir = $this->assetsResolver->uriToPath($options['targetUri']); } else { $uploadsDir = $this->assetsResolver->assetPath($assetFile->getType()); } $tempFile = $uploadedFile->move($uploadsDir, $this->getTargetFileName($uploadedFile->getClientOriginalName(), $uploadsDir)); $assetFile->setFile($tempFile); $uri = $this->assetsResolver->pathToUri($assetFile->getFile()->getPathname()); if ($uri === null) { throw new \RuntimeException('Unable to retrieve uploaded file uri'); } $assetFile->setUri($uri); return $assetFile; }
private function moveFile(UploadedFile $file, $id) { $destinationPath = $this->getDestinationPath() . '/' . $this->getPathById($id); if ($file->move($destinationPath, $file->getClientOriginalName())) { return $this->getPathById($id) . '/' . $file->getClientOriginalName(); } return ''; }
/** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function preUpload() { if (null === $this->file) { return; } $this->url = $this->file->guessExtension(); $this->alt = $this->file->getClientOriginalName(); }
/** * For files, we won't generate a very random name, * so we can easily. * * */ protected function uniqueName() { $filename = $this->file->getClientOriginalName(); $filename = str_replace(' ', '-', $filename); $pieces = explode('.', $filename); array_pop($pieces); return implode('.', $pieces); }
/** * Get the file name for the photo * * @return string */ public function fileName() { if (!is_null($this->name) && $this->name) { return $this->name; } $name = sha1($this->file->getClientOriginalName() . '-' . microtime()); $extension = $this->file->getClientOriginalExtension(); return "{$name}.{$extension}"; }
public function upload() { if (null === $this->file) { return; } $name = $this->file->getClientOriginalName(); $this->file->move($this->getUploadRootDir(), $name); $this->url = $name; }
/** * Check if all chunks of a file being uploaded have been received * If yes, return the name of the reassembled temporary file * * @param UploadedFile $uploadedFile * * @return UploadedFile|null */ public function getFileFromChunks(UploadedFile $uploadedFile) { $filename = time() . '-' . $uploadedFile->getClientOriginalName(); $path = $this->tmpDir . DIRECTORY_SEPARATOR . $filename; if (FlowBasic::save($path, $this->tmpDir)) { return new UploadedFile($path, $uploadedFile->getClientOriginalName(), $uploadedFile->getClientMimeType()); } return null; }
/** * Create a base file model class * * @return File */ protected function makeFileRecord() { $file = new File(); $file->fileable_type = $this->fileable_type; $file->fileable_id = $this->fileable_id; $file->filename = $this->timestamp . '-' . $this->file->getClientOriginalName(); $file->filetype = $this->file->getClientMimeType(); $file->filepath = $file->getURLPath() . $file->filename; $file->order = 0; return $file; }
public function run(UploadedFile $file, MediaProvider $media) { $media->ext = $file->getClientOriginalExtension(); $media->path = $file->getPath(); // by default /tmp $media->file = $file->getClientOriginalName(); $media->meta = empty($media->meta) ? [] : $media->meta; $media->sizes = empty($media->sizes) ? [] : $media->sizes; $media->caption = $file->getClientOriginalName(); $media->alt = $file->getClientOriginalName(); }
/** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function preUpload() { // Si jamais il n'y a pas de fichier (champ facultatif) if (null === $this->file) { return; } // Le nom du fichier est son id, on doit juste stocker �galement son extension // Pour faire propre, on devrait renommer cet attribut en � extension �, plut�t que � extension � $this->extension = $this->file->guessExtension(); // Et on g�n�re l'attribut alt de la balise <img>, � la valeur du nom du fichier sur le PC de l'internaute $this->alt = $this->file->getClientOriginalName(); }
public function playTorrentFileAction(Request $request) { $file = $_FILES['torrentFile']; $type = $_POST['torrentFileType']; $filename = $_POST['torrentFileName']; $uploadedFile = new UploadedFile($file['tmp_name'], $filename, $type); $moveTo = $this->get('kernel')->getRootDir() . '/cache/' . $this->get('kernel')->getEnvironment(); $uploadedFile->move($moveTo, $uploadedFile->getClientOriginalName()); $streamerService = $this->get('homesoft_torrent_streamer.torrent_streamer'); $response = array('status' => true, 'url' => $streamerService->startStreamer($moveTo . '/' . $uploadedFile->getClientOriginalName())); return new JsonResponse($response); }
public static function upload(UploadedFile $file, $uploadPath = null) { if (is_null($uploadPath)) { $uploadPath = public_path() . '/uploads/'; } $fileName = str_slug(getFileName($file->getClientOriginalName())) . '.' . $file->getClientOriginalExtension(); //Make file name unique so it doesn't overwrite any old photos while (file_exists($uploadPath . $fileName)) { $fileName = str_slug(getFileName($file->getClientOriginalName())) . '-' . str_random(5) . '.' . $file->getClientOriginalExtension(); } $file->move($uploadPath, $fileName); return ['filename' => $fileName, 'fullpath' => $uploadPath . $fileName]; }
/** * Upload new attachment * * @param UploadedFile $file * @param string $descriptionText * @param Language $language * @param array $attributes * @param Attachment $attachment * * @return Attachment */ public function upload(UploadedFile $file, $descriptionText, Language $language, array $attributes, Attachment $attachment = null) { $filesystem = new Filesystem(); $filesize = $file->getClientSize(); if ($filesize == false) { throw new FileException('File size is not valid'); } if (!file_exists($this->config['file_directory']) || !is_writable($this->config['file_directory'])) { throw new FileException('Directory ' . $this->config['file_directory'] . ' is not writable'); } if (!is_null($attachment)) { if ($filesystem->exists($this->getStorageLocation($attachment))) { $filesystem->remove($this->getStorageLocation($attachment)); } if ($descriptionText != $attachment->getDescription()->getTranslationText()) { $nextTranslationPhraseId = $this->em->getRepository('Newscoop\\Entity\\AutoId')->getNextTranslationPhraseId(); $description = new Translation($nextTranslationPhraseId); $description->setLanguage($language); $description->setTranslationText($descriptionText); $this->em->persist($description); } unset($attributes['description']); } else { $attachment = new Attachment(); $nextTranslationPhraseId = $this->em->getRepository('Newscoop\\Entity\\AutoId')->getNextTranslationPhraseId(); $description = new Translation($nextTranslationPhraseId); $description->setLanguage($language); $description->setTranslationText($descriptionText); unset($attributes['description']); $attachment->setCreated(new \DateTime()); $this->em->persist($description); $this->em->persist($attachment); } $attributes = array_merge(array('language' => $language, 'name' => $file->getClientOriginalName(), 'extension' => $file->getClientOriginalExtension(), 'mimeType' => $file->getClientMimeType(), 'contentDisposition' => Attachment::CONTENT_DISPOSITION, 'sizeInBytes' => $file->getClientSize(), 'description' => $description), $attributes); $this->fillAttachment($attachment, $attributes); if (is_null($attributes['name'])) { $attachment->setName($file->getClientOriginalName()); } $this->em->flush(); $target = $this->makeDirectories($attachment); try { $file->move($target, $this->getFileName($attachment)); $filesystem->chmod($target . '/' . $this->getFileName($attachment), 0644); } catch (\Exceptiom $e) { $filesystem->remove($target); $this->em->remove($attachment); $this->em->flush(); throw new \Exception($e->getMessage(), $e->getCode()); } return $attachment; }
/** * Handle the file upload. Returns the array on success, or false * on failure. * * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file * @param String $path where to upload file * @return array|bool */ public function handle(UploadedFile $file, $path = 'uploads') { $input = array(); $fileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)); // Detect and transform Croppa pattern to avoid problem with Croppa::delete() $fileName = preg_replace('#([0-9_]+)x([0-9_]+)#', "\$1-\$2", $fileName); $input['path'] = $path; $input['extension'] = '.' . $file->getClientOriginalExtension(); $input['filesize'] = $file->getClientSize(); $input['mimetype'] = $file->getClientMimeType(); $input['filename'] = $fileName . $input['extension']; $fileTypes = Config::get('file.types'); $input['type'] = $fileTypes[strtolower($file->getClientOriginalExtension())]; $filecounter = 1; while (file_exists($input['path'] . '/' . $input['filename'])) { $input['filename'] = $fileName . '_' . $filecounter++ . $input['extension']; } try { $file->move($input['path'], $input['filename']); list($input['width'], $input['height']) = getimagesize($input['path'] . '/' . $input['filename']); return $input; } catch (FileException $e) { Notification::error($e->getmessage()); return false; } }
public static function save($path, UploadedFile $file) { $ext = substr(strrchr($file->getClientOriginalName(), '.'), 1); $new_name = md5(time() . rand()) . '.' . $ext; $file->move($path, $new_name); return $new_name; }
protected function upload(UploadedFile $file, $oldFile) { $list = ""; // foreach($files as $file) // { // $validator = Validator::make( array('file' => $file) , array('file' => array($this->Rule) ) ); // // if($validator->fails()) // { //laravel內建的驗證無法使用(可能是bug吧),所以自己寫一個 foreach ($this->Rule as $rule) { if ($file->getClientOriginalExtension() == $rule) { if ($file->isValid()) { if ($this->groupno != "") { $year = substr($this->groupno, 1, 3); $destinationPath = public_path() . '/upload/' . $year . '/' . $this->groupno; } else { $destinationPath = public_path() . '/upload/teacher'; } $fileName = $file->getClientOriginalName(); File::delete($destinationPath . '/' . $oldFile); $file->move($destinationPath, $fileName); //用 "|" 隔開檔名 $list .= $fileName . "|"; } } } // } // } $list = substr($list, 0, -1); return $list; }
public static function upload(UploadedFile $file) { $fileMedia = new static(); $file->name = time() . '_' . strtolower($file->getClientOriginalName()); $file->move(config('admin.fileUploadDirectory'), $file->name); return $file; }
private function saveDisc(UploadedFile $file) { $fileName = time() . $file->getClientOriginalName(); $path = 'foto/'; $file->move($path, $fileName); return $fileName; }
/** * The main upload method. * * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file Uploaded file instance. * @return string */ public function upload(\Symfony\Component\HttpFoundation\File\UploadedFile $file) { // We simply move the uploaded file to the target directory $result = $file->move(\Config::get('plupload::plupload.upload_dir'), $file->getClientOriginalName()); // Return the result of the upload return $this->respond(array('OK' => $result ? 1 : 0)); }
public function upload(UploadedFile $file, PropertyMapping $propertyMapping, $filter_name = NULL) { if ($filter_name === NULL) { $filter_name = $propertyMapping->getMappingName(); } // vars $provider = $this->getProvider($propertyMapping); $filesystem = $provider->getFilesystem(); $relative_dir = $provider->getRelativeDir(); //$propertyMapping->getUriPrefix(); $file_name = $file->getClientOriginalName(); // if namer set $mapping_config = $this->vichGetMappingConfig($propertyMapping->getMappingName()); if ($mapping_config) { $namer = $mapping_config['namer']; // illegal offset warning (backwards compatibility) if (is_array($namer) && isset($namer['service'])) { $namer = $namer['service']; } if ($this->getContainer()->has($namer)) { $namer = $this->getContainer()->get($namer); if (method_exists($namer, 'getRandomFileName')) { $file_name = $namer->getRandomFileName($file_name, $propertyMapping); } } } // upload $uploaded = $filesystem->write($relative_dir . '/' . $file_name, file_get_contents($file->getPathname())); //$uploaded = $file->move($upload_dir, $file_name); if ($uploaded) { // apply filter return $this->doApplyFilter($file_name, $propertyMapping); } return false; }
/** * Generate a proper filename. * * @param UploadedFile $file * @return bool|string */ private function getFilename(UploadedFile $file) { $path = $file->getClientOriginalName(); $path .= '_' . dechex(time()); $path .= '.' . $file->getClientOriginalExtension(); return $path; }
/** * @param UploadedFile $uploadedFile */ function upload(UploadedFile $uploadedFile) { $path = sha1(uniqid(mt_rand(), true)) . '.' . $uploadedFile->guessExtension(); $this->setPath($path); $this->setName($uploadedFile->getClientOriginalName()); $uploadedFile->move($this->getUploadRootDir(), $path); }
/** * Upload a new resource. * * @param UploadedFile $file * @return string */ public function uploadResource(UploadedFile $file) { $destination_path = public_path('uploads' . DIRECTORY_SEPARATOR); $file_name = $this->generatePath($file->getClientOriginalName()); $file->move($destination_path, $file_name); return $file_name; }
public function upload(UploadedFile $file) { if ($file->isValid()) { $name = $file->getClientOriginalName(); $size = $file->getClientSize(); } }
/** * Resolve whether the file exists and if it already does, change the file name. * * @param string $folder * @param $file * @param bool $enableObfuscation * * @return array */ public function resolveFileName($folder, UploadedFile $file, $enableObfuscation = true) { if (!isset($file->fileSystemName)) { $file->fileSystemName = str_slug(basename($file->getClientOriginalName(), $file->getClientOriginalExtension())) . '.' . strtolower($file->getClientOriginalExtension()); } if (config('filer.obfuscate_filenames') && $enableObfuscation) { $fileName = basename($file->fileSystemName, $file->getClientOriginalExtension()) . '_' . md5(uniqid(mt_rand(), true)) . '.' . $file->getClientOriginalExtension(); } else { $fileName = $file->fileSystemName; } if (File::isFile($folder . $fileName)) { $basename = $this->getBasename($file); $pose = strrpos($basename, '_'); if ($pose) { $f = substr($basename, 0, $pose); $s = substr($basename, $pose + 1); if (is_numeric($s)) { ++$s; $basename = $f; } else { $s = 1; } } else { $s = 1; } $file->fileSystemName = $basename . '_' . $s . '.' . $file->getClientOriginalExtension(); return $this->resolveFileName($folder, $file, false); } return $file; }
/** * Saving and Database Recording Users Default Picture * @param UploadedFile $file Library for Uploading Files Locally or Cloud * @param User $user Users Model */ public function UserProfilePicture(UploadedFile $file, User $user) { $Image = $file->getClientOriginalName(); $Imagename = 'DP_' . $Image; $file->move(\Auth::User()->username . '/profile_images/', $Imagename); $user->userData()->update(['profile_picture' => '/' . \Auth::User()->username . '/profile_images/' . $Imagename, 'picture_name' => $Imagename]); }