cakePHP meioupload,将图像上传到每个模型的不同文件夹

发布于 2024-11-14 13:39:28 字数 2308 浏览 5 评论 0原文

我使用 meioupload 在 cakePHP 中上传图像,我使用一个名为“attachment”的表来保存上传图像的信息,这是我的附件表的结构:

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;

目前我还有另外 2 个表通过类字段(表名)和foreign_id。现在我的问题是,如何将上传的图像保存到每个模型的不同文件夹中?

例如:我想将我的帖子图像保存到“post”文件夹中,并将我的个人资料图像保存到我

中的“profile”文件夹更新:

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 :假设我目前有3个表,“附件”“帖子”和“个人资料”,充当meioupload的是“附件”,每次我通过“帖子”或“个人资料”上传图像时,我都会保存图像“attachment”中的“attachment”、foreign_id 和 class 字段中的信息是将“attachment”连接到“post”和“profile”的字段。

更新#3:我遵循 Dunhamzzz 关于动态使用行为的建议,并提出了这个解决方案,并且它有效。

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

谢谢

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 class 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走野 2024-11-21 13:39:28

答案就在您的 MeioUpload 中,特别是“dir”选项中,您可以放入 {ModelName}{fieldName} 来更改文件的保存位置。这是行为本身的默认值:

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

更新

为了让MeioUpload支持同一模型的不同设置,您可以尝试动态附加行为,它允许您根据需要更改设置。

例如,在您的帖子中,

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

请务必阅读文档中有关行为的部分,它有望帮助您在每个操作的基础上制定解决方案,而不是在行为附带的每个模型上制定解决方案。

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.

半葬歌 2024-11-21 13:39:28

以下是 $actAs 数组的示例。

    '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'
            ),
        )
    ),

如您所见,有一个可以修改的键“dir”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文