开发者

cakePHP meioupload, upload image to a different folder for each model

开发者 https://www.devze.com 2023-03-12 02:27 出处:网络
I use meioupload to upload images in cakePHP, i use a table called \'attachment\' to save the uploaded image\'s information, this is the structure of my attachment table:

I use meioupload to upload images in cakePHP, i use a table called 'attachment' to save the uploaded image's information, this is the structure of my attachment table:

CREATE TABLE IF NOT EXISTS `attachments` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `class` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `foreign_id` bigint(20) unsigned NOT NULL,
  `filename` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `dir` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `mimetype` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `filesize` bigint(20) DEFAULT NULL,
  `height` bigint(20) DEFAULT NULL,
  `width` bigint(20) DEFAULT NULL,
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

And i currently have another 2 tables connected with this through the class field (table name) and foreign_id. Now my question is, how can i save the uploaded image to a different folder for each model?

For example: i would like to save my post's image into 'post' folder and to save my profile image into 'profile' folder

UPDATE: in my attachment model

public $actsAs = array(
    'MeioUpload' => array(
        'filename' => array(
            'dir' => 'post', #i set the default folder as 'post' at the moment
            'create_directory' => true,
            'allowed_mime' => array(
                'image/jpeg',
                'image/pjpeg',
                'image/png'
            ),
            'allowed_ext' => array(
                '.jpg',
                '.jpeg',
                '.png'
            ),
            'thumbsizes' => array(                  
                'large' => array(
                    'width' => 500,
                    'height' => 500
                ),
                'small' => array(
                    'width' => 100,
                    'height' => 100
                )
            )
        )
    )
);

UPDATE #2 : let say that i currently have 3 tables, "attachment" "post" and "profile", the one that actAs meioupload is "attachment", every time i upload an image through "post" or "profile", i will save the image information into "attachment", foreign_id and cla开发者_如何学JAVAss fields in "attachment" is the one connecting "attachment" to "post" and "profile".

UPDATE #3 : i followed Dunhamzzz suggestion on using the behavior on the fly, and come up with this solution, and it works.

$this->Attachment->Behaviors->attach(
    'MeioUpload', array(
        'filename' => array(
            'dir' => 'avatars'
        )
    ));

Thanks


The answer is in your MeioUpload, specifically the 'dir' option, you can put {ModelName} or {fieldName} in to alter where the file saves. Here is the default in the behaviour itself:

dir' => 'uploads{DS}{ModelName}{DS}{fieldName}',

Update

In order to get MeioUpload to support different settings for the same model, you could try attaching the behaviour on the fly, which allows you to change the settings as you please.

eg in your posts action

$this->Attachment->Behaviours->attach('MeioUpload', array('dir' => '/uploads/posts/');

Be sure to read the part on behaviours on the docs, it will hopefully help you work out a solution on a per-action basis as opposed to per-model which comes with behaviours.


Here is an example for the $actAs array.

    'MeioUpload' => array(
        'filename' => array(
            'dir' => 'files/banners',
            'create_directory' => false,
            'allowed_mime' => array(
                'image/jpeg',
                'image/pjpeg',
                'image/gif',
                'image/png'
            ),
            'allowed_ext' => array(
                '.jpg',
                '.jpeg',
                '.png',
                '.gif'
            ),
        )
    ),

as you can see, there is a key "dir" that you can modify

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号