我使用 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
)
)
)
)
);
更新#2:假设我目前有3个表,“附件”“帖子”和“个人资料”,作为meioupload的那个是“附件”,每次我通过“帖子”或“个人资料”上传图片时,我将图片信息保存到“attachment”中,“attachment”中的foreign_id和class字段是“attachment”与“post”和“profile”的连接。
更新#3:我遵循了 Dunhamzzz 关于动态使用行为的建议,并提出了这个解决方案,并且它有效。
$this->Attachment->Behaviors->attach(
'MeioUpload', array(
'filename' => array(
'dir' => 'avatars'
)
));
谢谢