【问题标题】:cakePHP meioupload, upload image to a different folder for each modelcakePHP meioupload,为每个模型上传图片到不同的文件夹
【发布时间】:2011-06-10 08:26:37
【问题描述】:

我在 cakePHP 中使用 meioupload 上传图片,我使用一个名为 '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
                )
            )
        )
    )
);

更新 #2 :假设我目前有 3 个表,“附件”“帖子”和“个人资料”,作为 meioupload 的表是“附件”,每次我上传图片通过“post”或“profile”,我将图像信息保存到“attachment”中,“attachment”中的foreign_id和class字段是连接“attachment”到“post”和“profile”的字段。

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

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

谢谢

【问题讨论】:

  • 请从您的模型中发布您的 $actAs 代码。
  • @alex 我更新了我的问题

标签: cakephp image-upload meio-upload


【解决方案1】:

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

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

更新

为了让MeioUpload支持同一型号的不同设置,你可以试试attaching the behaviour on the fly,它可以让你随意更改设置。

例如在您的帖子操作中

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

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

【讨论】:

  • 嗯这不是我的意思,如果我这样做 {ModelName},它只会将图像上传到“附件”文件夹,因为这个行为是“附件”模型,我会然后更新我的问题。
  • @dunhamzz 应该附在哪里?我尝试在我的控制器中使用它,但它不起作用
  • 我猜想将它附加在控制器操作中而不是 $actsAs 中,只需确保在附加时将所有设置传递给它。
  • 哦,这是一个错字,应该是“行为”而不是“行为”我想这就是我收到错误消息的原因,我会再试一次,我会告诉你的。谢谢
  • 它有效,我需要一些修改,感谢您指出这一点
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多