/** * 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; }
/** * {@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); } }
/** * 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]); }
public function testShouldSetEmptyUploadedFilesToNull() { $bag = new FileBag(array('file' => array( 'name' => '', 'type' => '', 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0 ))); $this->assertNull($bag->get('file')); }
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, []); }
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); }
/** * 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; }