私は 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;
そして、現在、クラスフィールド(テーブル名)とforeign_idを介してこれに接続された別の2つのテーブルがあります。今私の質問は、アップロードされた画像をモデルごとに異なるフォルダーに保存するにはどうすればよいですか?
例: 投稿の画像を「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:現在、「投稿」または「プロファイル」を介して画像をアップロードするたびに、actAs meiouploadが「添付ファイル」である「添付ファイル」「投稿」および「プロファイル」の3つのテーブルがあるとしましょう。画像情報を「attachment」に保存します。「attachment」のforeign_idおよびclassフィールドは、「attachment」を「post」および「profile」に接続するものです。
更新#3:私はその場で動作を使用することに関するDunhamzzzの提案に従い、この解決策を考え出し、それが機能しました。
$this->Attachment->Behaviors->attach(
'MeioUpload', array(
'filename' => array(
'dir' => 'avatars'
)
));
ありがとう