El campo de carga de múltiples archivos de CakePHP solo carga una imagen

I am trying to upload some files using the multiple upload file field. The POST information gets sent correctly and looks like this:

Array
(
    [Uploads] => Array
        (
            [photos] => Array
                (
                    [0] => Array
                        (
                            [name] => image - Copy - Copy.jpg
                            [type] => image/jpeg
                            [tmp_name] => /tmp/phpALAMwT
                            [error] => 0
                            [size] => 60892
                        )

                    [1] => Array
                        (
                            [name] => image - Copy.jpg
                            [type] => image/jpeg
                            [tmp_name] => /tmp/phpoIGtta
                            [error] => 0
                            [size] => 60892
                        )

                    [2] => Array
                        (
                            [name] => image.jpg
                            [type] => image/jpeg
                            [tmp_name] => /tmp/phpERTogu
                            [error] => 0
                            [size] => 60892
                        )

                )

And i loop through and insert each one into the database, then upload them using the ID from the database, like this:

// Upload Photos
if (!empty($this->request->data['Uploads']['photos'][0]['tmp_name'])){

    foreach($this->request->data['Uploads']['photos']as $photo){

        $property_id = $this->request->data['Property']['ID'];
        $file_name = $photo['name'];
        $file_size = $photo['size'];
        $file_ext = pathinfo($photo['name'], PATHINFO_EXTENSION);

        // Save to DB
        $this->Property->PropertyImage->save(array(
            'PropertyImage' => array("Live"=>1, 'Number'=>99, "Type"=>'L', "FileType"=>$file_ext, "PropertyID"=>$property_id, 'Source'=>$file_name, 'Size=>'.$file_size)
        ));

        // Upload
        $id = $this->Property->PropertyImage->getLastInsertID();
        $path = intval($id/1000) . '/' . $id . '.' . $file_ext;
        move_uploaded_file($photo['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/imgp/F/'.$path);

    }
}

But only one image gets put into the database and uploaded each time, cant work out why the look isn't working right.

¿Algunas ideas? Gracias.

preguntado el 09 de marzo de 12 a las 16:03

1 Respuestas

Before the line were you call save()

$this->Property->PropertyImage->save(...)

llámenos al

$this->Property->PropertyImage->create();

to tell the model to write a new record instead of continue to work with the just saved one.

respondido 09 mar '12, 16:03

Why should it stop? I would also recommend a better way to store the images if you expect lots of them. I recently wrote an article about that: cakedc.com/florian_kraemer/2012/02/03/… - floriank

Do you have the new link to the article? - Tenga

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.