/** * Upload an image. * @param \Symfony\Component\HttpFoundation\FileBag $p_file * @param string $p_rootDir * @param string $p_basePath * @param string $p_folder * @param string $p_path * @return \Symfony\Component\HttpFoundation\JsonResponse * @throws \Exception */ public function uploadImage(FileBag $p_file, $p_rootDir, $p_basePath, $p_folder, $p_path) { $arrExtension = array("gif", "jpeg", "jpg", "png"); $folder = $this->obtainFolder($p_rootDir, $p_folder); $path = $this->obtainPath($p_basePath, $p_path); $response = new JsonResponse(); // ------------------------- DECLARE ---------------------------// if ($p_file == null) { $response->setData(array("error" => "No file received.")); return $response; } $file = $p_file->get("file"); if ($file == null) { $response->setData(array("error" => "No file received.")); return $response; } if ($file->getSize() > UploadedFile::getMaxFilesize()) { $response->setData(array("error" => "File too big.")); return $response; } // Cheks image type. $extension = $file->guessExtension(); $mime = $file->getMimeType(); if (($mime == "image/gif" || $mime == "image/jpeg" || $mime == "image/pjpeg" || $mime == "image/x-png" || $mime == "image/png") && in_array($extension, $arrExtension)) { // Generates random name. $name = sha1(uniqid(mt_rand(), true)) . '.' . $file->guessExtension(); // Save file in the folder. $file->move($folder, $name); $response->setData(array("link" => $path . '/' . $name)); return $response; } $response->setData(array("error" => "File not supported.")); return $response; }
public function testShouldNotConvertNestedUploadedFiles() { $tmpFile = $this->createTempFile(); $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0); $bag = new FileBag(array('image' => array('file' => $file))); $files = $bag->all(); $this->assertEquals($file, $files['image']['file']); }
/** * {@inheritdoc} */ protected function handleFiles(FileBag $files, $project) { $image = $files->get('image'); if ($image instanceof UploadedFile) { $media = $this->mediaFactory->create($image); $this->uploader->upload($media); $project->setImage($media); } }
/** * @param \Symfony\Component\HttpFoundation\FileBag $fileBag * @param int $id * * @return \Doctrine\Common\Collections\ArrayCollection */ public function add(FileBag $fileBag, $id) { $urls = new ArrayCollection(); foreach ($fileBag->all() as $file) { if ($file instanceof UploadedFile) { $uri = $this->processUploadedFile($file, $id); $urls->add($uri); } } return $urls; }
/** * Flattens a given filebag to extract all files. * * @param bag The filebag to use * @return array An array of files */ protected function getFiles(FileBag $bag) { $files = array(); $fileBag = $bag->all(); $fileIterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($fileBag), \RecursiveIteratorIterator::SELF_FIRST); foreach ($fileIterator as $file) { if (is_array($file) || null === $file) { continue; } $files[] = $file; } return $files; }
public function uploadFileAction() { /** @var UploadPathProvider $uploadPathProvider */ $uploadPathProvider = $this->get('swag_import_export.upload_path_provider'); $fileBag = new FileBag($_FILES); $clientOriginalName = ''; /** @var UploadedFile $file */ foreach ($fileBag->getIterator() as $file) { $clientOriginalName = $file->getClientOriginalName(); $file->move($uploadPathProvider->getPath(), $clientOriginalName); } $this->view->assign(['success' => true, 'data' => ['path' => $uploadPathProvider->getRealPath($clientOriginalName), 'fileName' => $clientOriginalName]]); }
function it_handles_form_that_tests_handle_project_image(Request $request, ProjectInterface $project, FormFactory $formFactory, FormBuilderInterface $formBuilder, FormInterface $form, FileBag $fileBag, MediaFactory $mediaFactory, MediaInterface $media, ObjectManager $manager, MediaUploader $uploader) { $image = new UploadedFile('', '', null, null, 99, true); // Avoids file not found exception $formFactory->createNamedBuilder('', 'kreta_project_project_type', $project, [])->shouldBeCalled()->willReturn($formBuilder); $formBuilder->getForm()->shouldBeCalled()->willReturn($form); $request->isMethod('POST')->shouldBeCalled()->willReturn(true); $form->handleRequest($request)->shouldBeCalled(); $form->isValid()->shouldBeCalled()->willReturn(true); $fileBag->get('image')->shouldBeCalled()->willReturn($image); $request->files = $fileBag; $mediaFactory->create($image)->shouldBeCalled()->willReturn($media); $uploader->upload($media)->shouldBeCalled(); $project->setImage($media)->shouldBeCalled()->willReturn($project); $manager->persist($project)->shouldBeCalled(); $manager->flush()->shouldBeCalled(); $this->handleForm($request, $project, []); }
/** * @param Conversation $conversation * @param FileBag $fileBag */ public function addConversationImage($conversation, FileBag $fileBag) { if ($fileBag->count() > $this->maxNumCnvImage) { throw new HttpException(Codes::HTTP_BAD_REQUEST, "Too many Images"); } $urls = $this->fileUploadUtil->add($fileBag, "private"); $conversationImages = new ArrayCollection(); foreach ($urls as $url) { $conversationImage = new ConversationImage(); $conversationImage->setUrl($url); $conversationImage->setConversation($conversation); $conversationImages->add($conversationImage); } $conversation->setImages($conversationImages); if ($conversationImages->count() > 0) { $this->helper->persist($conversation); } }
/** * Execute the job. * * @return void */ public function handle() { // /** // * Create Product // */ // $product = $product->create([ // 'name' => $this->request->get('name'), // 'code' => $this->request->get('code'), // 'image' => $this->moveFile($this->image, '/image/products/', 'figurine'), // ]); // // /** // * Create Product Profile // */ // $product->profile()->create([ // 'description' => $this->request->get('description'), // 'image' => $this->moveFile($this->poster, '/image/products/', 'poster') // ]); $this->product->update(['name' => $this->request->get('name'), 'code' => $this->request->get('code'), 'image' => $this->files->has('image') ? $this->moveFile($this->files->get('image'), '/image/products/', 'figurine') : $this->product->image]); $this->product->profile->update(['description' => $this->request->get('description'), 'image' => $this->files->has('poster') ? $this->moveFile($this->files->get('poster'), '/image/products/', 'poster') : $this->product->profile->image]); }
/** * Retrieve a file from the request. * * @param string $key * @param mixed $default * @return \Symfony\Component\HttpFoundation\File\UploadedFile|array|null */ public function file($key = null, $default = null) { // return data_get($this->allFiles(), $key, $default); return Arr::get($this->files->all(), $key, $default); }
public function uploadAction() { $root = Shopware()->Container()->getParameter('kernel.root_dir'); $root .= '/engine/Shopware/Plugins/Community'; if (!is_writable($root)) { $this->View()->assign(['success' => false, 'message' => 'Plugin Community directory is not writable']); return; } try { $fileBag = new FileBag($_FILES); /** @var $file UploadedFile */ $file = $fileBag->get('plugin'); } catch (Exception $e) { $this->View()->assign(['success' => false, 'message' => $e->getMessage()]); return; } $information = pathinfo($file->getClientOriginalName()); if ($information['extension'] !== 'zip') { $this->View()->assign(['success' => false, 'message' => 'Wrong archive extension %s. Zip archive expected']); unlink($file->getPathname()); unlink($file); return; } $name = $information['basename']; $path = $root . '/' . $name; try { $file->move($root, $name); $extractor = new PluginExtractor(); $extractor->extract($path, $root); unlink($path); unlink($file->getPathname()); unlink($file); } catch (Exception $e) { $this->View()->assign(['success' => false, 'message' => $e->getMessage()]); unlink($path); unlink($file->getPathname()); unlink($file); $this->View()->assign(['success' => false, 'message' => $e->getMessage()]); return; } $this->View()->assign('success', true); }
/** * @param FileBag|UploadedFile[] $images * @param Product $product * * @throws \Symfony\Component\HttpFoundation\File\Exception\FileException */ public function attachUploadedImagesToProduct($images, Product $product) { $product->attachImages(array_map(function (UploadedFile $image) { return $this->imageRepository->storeUploadedImage($image)->id; }, $images instanceof FileBag ? $images->all() : (array) $images)); }
/** * Get array of file info from file object * * @return array */ protected function getFilesInfoArray(FileBag $files) { $filesArray = array(); $file = $files->get($this->fileField); $fileInfo = array(); $fileInfo['name'] = $file->getClientOriginalName(); $fileInfo['type'] = $file->getMimeType(); $fileInfo['tmp_name'] = $file->getPathName(); $fileInfo['error'] = $file->getError(); $fileInfo['size'] = $file->getSize(); $filesArray['file'] = $fileInfo; return $filesArray; }