Multiple image upload - Meio Upload

1.4k views Asked by At

I am using the 'MeioUpload' plugin found here 'https://github.com/jrbasso/MeioUpload' and Cakephp 2.x.

Currently using this for single image uploads, please can anyone give advice on how to handle multiple image uploads using this plugin. Currently the db table storing the images holds filename, dir, mimetype and filesize fields for each image. I want to store more than one image for each of my posts when adding a new post. Any help would be much appreciated, thanks in advance :).

1

There are 1 answers

1
joshua.paling On BEST ANSWER

As I mentioned in my comment, you might want to try https://github.com/josegonzalez/upload as MeioUpload is now deprecated, and it's developer is working on that new upload plugin I linked to.

Either way, the following info for MeioUpload holds true for the new plugin, too.

MeioUpload is built to handle one uploaded file per corresponding set of fields. I don't think the example in MeioUpload's ReadMe is ideal, as it seems to imply that you have to have a table of 'images', where as in reality, you can have a table of just about anything, where each record holds one or more uploaded files (be it images, PDF's, MP3's... anything).

So, with that in mind, you have two solutions:

1) If your posts will have a potentially infinite number of images (ie, not a fixed, small number) then you can have Posts and Images in separate tables, and set up a hasMany relationship between them. See http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

2) If you know that each post will only have a max of say 3 or 4 (or some other relatively small number) of images, then you can implement 3 (or 4, or X) sets of image fields in your Posts table / model, each to handle a separate upload. They'd be named, eg. featured_image_filename, feautred_image_dir, etc; image2_filename, image2_dir, image2_mimetype, etc; image3_filename, image3_dir, etc.

Your acts as would look something like:

var $actsAs = array(
    'MeioUpload.MeioUpload' => array(
        'featured_image_filename' => array(
                'fields' => array(
                    'dir' => 'featured_image_dir',
                    'filesize' => 'featured_image_filesize',
                    'mimetype' => 'featured_image_mimetype'
                ),
        ),
        'image2_filename' => array(
                'fields' => array(
                    'dir' => 'image2_dir',
                    'filesize' => 'image2_filesize',
                    'mimetype' => 'image2_mimetype'
                ),
        ),
        'image3_filename' => array(
                'fields' => array(
                    'dir' => 'image3_dir',
                    'filesize' => 'image3_filesize',
                    'mimetype' => 'image3_mimetype'
                ),
        ),
    )
);

This second solution is hardly ideal database design, but sometimes when you know there'll never be more than a few images, it's just the easiest way to do it - both in terms of developing, and in terms of an easy to use UI.

Make sense?