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

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个表通过类字段(表名称)和firree_id连接。现在我的问题是,如何将上传的图像保存到每个型号的其他文件夹中?

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

更新: 在我的附件模型中

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
                )
            )
        )
    )
);

更新#2 :说我目前有3个表格,“附件”“ post”和“配置文件”,每次我通过“ post”或“ profile”上传映像时,我都会保存该图像“附件”。 “附件”中的“附件”,外_id和类字段中的图像信息是“连接”连接到“ post”和“ profile”。

更新#3:我遵循了Dunhamzzz关于即时使用该行为的建议,并提出了此解决方案,并且可以使用。

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

谢谢

有帮助吗?

解决方案

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

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

更新

为了让Meioupload支持同一型号的不同设置,您可以尝试 飞行行为, ,这使您可以随意更改设置。

例如在您的帖子行动中

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

请务必阅读有关文档行为的一部分,希望您以每项行为而不是每种模型来帮助您制定解决方案。

其他提示

这是$ 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”

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top