0

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

2
  • Do you want to fetch this data ? your question and question details looking difference. Commented Apr 7, 2016 at 11:30
  • no, I am trying to insert data. but when getting data it is in this format Commented Apr 7, 2016 at 11:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, I want data in the format you explained. I am trying to send files in the following keys Report[0],Report[1] and so on. To get the explained format what I need to change in my key
Hi Kapil, I extended my answer, covering this aspect as well. You will find more information in the documentation about naming form fields for savemany-tasks: book.cakephp.org/2.0/en/core-libraries/helpers/form.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.