Get Uploaded File's Original Name

前端 未结 4 1502
日久生厌
日久生厌 2020-12-31 06:23
$request = $event->getRequest();
print_r($request->files);die;

gives me

Symfony\\Component\\HttpFoundation\\FileBag Object
(
         


        
相关标签:
4条回答
  • 2020-12-31 07:07

    When you upload files you get UploadFile (API link) objects (basically the wrappers of array).

    $this->getRequest()->files[0]->getClientOriginalName();
    

    Can't try this now but you might need to do this instead:

    $this->getRequest()->files['name_of_file_field_in_post']->getClientOriginalName();
    

    where you would replace name_of_file_field_in_post with your form field's name.

    0 讨论(0)
  • 2020-12-31 07:14

    I'm using symfony 4,

    I wanted to get the files original name. What worked for me is:

    $form = $this->createForm(SomeType::class, $object);
    
    if($form->isSubmitted() && $form->isValid()) {
    
        $fileName = $object->getImage()->getClientOriginalName();
    
    }
    

    In my case getImage() is the getter for the name of the file upload field. So that would change to your relevant getter function for the file upload field.

    0 讨论(0)
  • 2020-12-31 07:20

    2015 Update:

    $request->files->get('your-file-name')->getClientOriginalName();
    

    your-file-name for me was just file.

    This will probably help too: https://github.com/1up-lab/OneupUploaderBundle/issues/21

    0 讨论(0)
  • 2020-12-31 07:28

    This wound up working for me, I guess the OneUp Class handles it a bit differently

    use Oneup\UploaderBundle\Event\PostPersistEvent;
    class UploadListener
    {
         public function onUpload(PostPersistEvent $event)
        {
    
            $request = $event->getRequest();
            $original_filename = $request->files->get('blueimp')->getClientOriginalName();
         }
    }
    

    Relevent Frontend

    <input id="fileupload" type="file" name="blueimp" data-url="{{ oneup_uploader_endpoint('images') }}" multiple />
    
    0 讨论(0)
提交回复
热议问题