【发布时间】: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