简体   繁体   中英

CakePHP multiple file upload field only uploading one image

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.

Any ideas? Thanks.

Before the line were you call save()

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

call

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM