Access $ _FILE ['tmp_name'] from the UploadedFile class?

if I print the contents of the UploadedFile instance, this is what I get

array ( 'opt_image_header' => Symfony\Component\HttpFoundation\File\UploadedFile::__set_state(array( 'test' => false, 'originalName' => 'triangle-in-the-mountains.jpg', 'mimeType' => 'image/jpeg', 'size' => 463833, 'error' => 0, ) 

And this is how I get the downloaded file in the controller. Before moving it, I have to resize it.

  foreach($request->files as $uploadedFile){ $ext = '.' . $uploadedFile['opt_image_header']->guessExtension(); $filename = sha1(uniqid(mt_rand(), true)) . $ext; $uploadedFile['opt_image_header']->move($path . '/images/', $filename); } 

so there is no "tmp_name" that I need to resize the image before saving it.

Do I need to read it directly from the $ _FILE array?

+8
upload image symfony
source share
2 answers

Use $uploadedFile->getRealPath()

Symfony\Component\HttpFoundation\File\UploadedFile extends Symfony\Component\HttpFoundation\File\File , which extends PHP SplFileInfo , so UploadedFile inherits all methods from SplFileInfo .

Use $uploadedFile->getRealPath() for the absolute path for the file. You can also use other methods, for example getFilename() or getPathname() . For a complete list of available methods ( SplFileInfo ), see the docs .

The Symfony File class adds additional methods such as move() and getMimeType() , and adds backward compatibility for getExtension() (which was not available before PHP 5.3.6). UploadedFile adds some additional methods on top of this, such as getClientOriginalName() and getClientSize() , which provide the same information that you usually received from $_FILES['name'] and $_FILES['size'] .

+17
source share

If you upload a file using Doctrine, see the Symfony Documentation Download the file
If you want to download a file without Doctrine, you can try something like:

 foreach($request->files as $uploadedFile) { $filename = $uploadedFile->get('Put_Input_Name_Here')->getClientOriginalName(); $file = $uploadedFile->move($distination_path, $filename); } 

If there is a problem loading the move() file, an exception will be thrown

UPDATED
In order for the temporary path of the downloaded file to resize the image, you can use the getPath() function in the specified loop

 $tmp_name = $uploadedFile->get('Put_Input_Name_Here')->getPath(); 

If you ask why, since the Symfony File class extends SplFileInfo

+1
source share

All Articles