Reputation: 1048
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
)
)
)
Upvotes: 0
Views: 1030
Reputation: 1428
I am not entirely sure I got your question right but I think one problem lies in the data format. I assume you are using the savemany
method. The data is expected in this format:
$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)
);
Basically you are providing the data field by field instead of record by record. I don't think that cake can process this correctly.
To get data in the desired format you can either loop through the data or use the convenience method Hash that cakephp provides, e.g. by using extract
on the required keys/values.
Receive the data in the right format
If you are able to change the submit-form you name the file input fields like this. The result should be the desired format.
<input type="file" name="Report.0">
<input type="file" name="Report.1">
This will result in the format:
[Reports] => Array
(
[0] => Array
(
[name] => 'Chrysanthemum.jpg'
[type] => 'image/jpeg'
)
[1] => Array
(
[name] => 'Jellyfish.jpg'
[type] => 'image/jpeg'
)
)
You should use MODELNAME.{n}.FIELDNAME as naming for form fields in Cakephp, though. So if reports is your model it would make sense having a field name for your file-field.
Upvotes: 1