获取文件的原始名称

时间:2023-01-26 14:52:55

I have a problem to get the orginal name of a file uploaded byoneupuploaderbundle

我有一个问题是获取由oneupuploaderbundle上传的文件的原始名称

If i use

如果我使用

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

(See this post Get Uploaded File's Original Name ) I have this error :

(请参阅此文章获取上载文件的原始名称)我有这个错误:

FatalErrorException: Error: Cannot use object of type Symfony\Component\HttpFoundation\FileBag as array in /home/.../UploadListener.php line 17

FatalErrorException:错误:无法在/home/.../UploadListener.php第17行中使用Symfony \ Component \ HttpFoundation \ FileBag类型的对象作为数组

but i think is correct, see the var_dump :

但我认为是正确的,请参阅var_dump:

$request = $event->getRequest();
var_dump($request->files);

give :

object(Symfony\Component\HttpFoundation\FileBag)#11 (1) {
  ["parameters":protected]=>
  array(1) {
    ["file"]=>
    array(1) {
      [0]=>
      object(Symfony\Component\HttpFoundation\File\UploadedFile)#12 (7) {
        ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
        bool(false)
        ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
        string(22) "silk_icons_preview.png"
        ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
        string(9) "image/png"
        ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
        int(116463)
        ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
        int(0)
        ["pathName":"SplFileInfo":private]=>
        string(14) "/tmp/php3GUXwQ"
        ["fileName":"SplFileInfo":private]=>
        string(9) "php3GUXwQ"
      }
    }
  }
}

Thanks !

2 个解决方案

#1


You have to do that :

你必须这样做:

$this->getRequest()->files->get('file')->getClientOriginalName();

The 'file' value is the name of your file input.

'file'值是文件输入的名称。

#2


My function :

我的功能:

 public function onUpload(PostPersistEvent $event) {
      $test = $event->getRequest()->files->get('upload')->getClientOriginalName();
      var_dump($test);
    }

My form :

我的表格:

<form action="{{ oneup_uploader_endpoint('gallery') }}" class="dropzone" name="upload" type="file" method="post" id="doc"></form>

Do :

FatalErrorException: Error: Call to a member function getClientOriginalName() on a non-object in /home/www/.../UploadListener.php line 19

FatalErrorException:错误:在/home/www/.../UploadListener.php第19行中的非对象上调用成员函数getClientOriginalName()

#1


You have to do that :

你必须这样做:

$this->getRequest()->files->get('file')->getClientOriginalName();

The 'file' value is the name of your file input.

'file'值是文件输入的名称。

#2


My function :

我的功能:

 public function onUpload(PostPersistEvent $event) {
      $test = $event->getRequest()->files->get('upload')->getClientOriginalName();
      var_dump($test);
    }

My form :

我的表格:

<form action="{{ oneup_uploader_endpoint('gallery') }}" class="dropzone" name="upload" type="file" method="post" id="doc"></form>

Do :

FatalErrorException: Error: Call to a member function getClientOriginalName() on a non-object in /home/www/.../UploadListener.php line 19

FatalErrorException:错误:在/home/www/.../UploadListener.php第19行中的非对象上调用成员函数getClientOriginalName()