3

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.

1 Answer 1

2

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.

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

3 Comments

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/…
Do you have the new link to the article?

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.