cakephp rest api multiple file uploading

时间:2016-04-07 10:53:50

标签: php rest cakephp cakephp-2.3

I trying to upload multiple filesby REST API in cakephp V2.3. I am running api by postman addon of chrome. The issue is the format of array of files. Below is the format I am getting. It is hard to get data from the above array. Please guide me how set key to get value in standard format.

Array
(
    [Reports] => Array
        (
            [name] => Array
                (
                    [0] => Chrysanthemum.jpg
                    [1] => Jellyfish.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpDZoRzW
                    [1] => /tmp/phpVyb98b
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 879394
                    [1] => 775702
                )

        )

)

enter image description here

1 个答案:

答案 0 :(得分:1)

我不完全确定我的问题是对的,但我认为问题在于数据格式。我假设您使用的是savemany方法。预计数据采用以下格式:

$data = array(
    array('name' => 'Chrysanthemum.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/phpDZoRzW', 'error' => 0, 'size' => 879394),
    array('name' => 'Jellyfish.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/phpVyb98b', 'error' => 0, 'size' => 775702)
);

基本上,您按字段提供数据字段而不是逐个记录。我认为蛋糕不能正确处理。

要获得所需格式的数据,您可以循环访问数据或使用cakephp提供的便捷方法Hash,例如:在所需的键/值上使用extract

以正确的格式接收数据

如果您能够更改提交表单,则将文件输入字段命名为this。结果应该是所需的格式。

<input type="file" name="Report.0">
<input type="file" name="Report.1">

这将产生以下格式:

   [Reports] => Array
        (
            [0] => Array
                (
                    [name] => 'Chrysanthemum.jpg'
                    [type] => 'image/jpeg'
                )
            [1] => Array
                (
                    [name] => 'Jellyfish.jpg'
                    [type] => 'image/jpeg'
                )
        )

你应该使用MODELNAME。{n} .FIELDNAME作为Cakephp中表单字段的命名。因此,如果报告是您的模型,则为文件字段指定字段名称是有意义的。