附件/attach

附件插件,让 CakePHP 2.0 的上传变得简单

安装: 133

依赖: 0

建议者: 0

安全: 0

星标: 38

关注者: 6

分支: 22

开放问题: 2

类型:cakephp-plugin

1.0.3 2014-11-24 13:55 UTC

This package is not auto-updated.

Last update: 2024-09-24 05:00:07 UTC


README

Attach 是一个 CakePHP 2.0 插件,可以让上传变得简单!

Attach 包含一个行为,为你完成所有操作,上传文件,并调整图片大小。

要求

  • PHP 5.3 或更高版本
  • CakePHP 2.0 或更高版本

安装

  • 从 GitHub 克隆:在应用程序目录中输入 git clone git@github.com:krolow/Attach.git Plugin/Attach
  • 从 GitHub 下载存档并将其解压缩到 app/Plugin/Attach
  • 如果你需要生成缩略图,你应该使用 composer 安装依赖项,并确保在 CakePHP 应用程序中调用 composer 的自动加载

用法

在需要上传的模型中,将类声明替换为以下类似内容

重要的是要记住,你的模型类可以有自己的字段,并且它将与具有上传字段的 Attachment 模型有一个额外的关联。

<?php
	App::uses('AppModel', 'Model');

	class Media extends AppModel {

		public $validate = array(
			'image' => array(
				'extension' => array(
					'rule' => array(
						'extension', array(
							'jpg',
							'jpeg',
							'bmp',
							'gif',
							'png',
							'jpg'
						)
					),
					'message' => 'File extension is not supported',
					'on' => 'create'
				),
				'mime' => array(
					'rule' => array('mime', array(
						'image/jpeg',
						'image/pjpeg',
						'image/bmp',
						'image/x-ms-bmp',
						'image/gif',
						'image/png'
					)),
					'on' => 'create'
				),
				'size' => array(
					'rule' => array('size', 2097152),
					'on' => 'create'
				)
			),
			'swf' => array(
				'extension' => array(
					'rule' => array(
						'extension', array(
							'swf',
						)
					),
					'message' => 'File extension is not supported',
					'on' => 'create'
				),
				'mime' => array(
					'rule' => array('mime', array(
						'application/x-shockwave-flash',
					)),
					'on' => 'create'
				),
				'size' => array(
					'rule' => array('size', 53687091200),
					'on' => 'create'
				)
			),
			'zip' => array(
				'extension' => array(
					'rule' => array(
						'extension', array(
							'zip',
						)
					),
					'message' => 'File extension is not supported',
					'on' => 'create'
				),
				'mime' => array(
					'rule' => array('mime', array(
						'application/zip',
						'multipart/x-zip'
					)),
					'on' => 'create'
				),
				'size' => array(
					'rule' => array('size', 53687091200),
					'on' => 'create'
				)
			),
		);

		public $actsAs = array(
			'Attach.Upload' => array(
				'Attach.type' => 'Imagick', //you can choose btw Imagick or Gd to handle the thumbnails, in case you do not pass that default is GD
				'swf' => array(
				    'dir' => 'webroot{DS}uploads{DS}media{DS}swf'
				),
				'image' => array(
				    'dir' => 'webroot{DS}uploads{DS}media{DS}image',
				    'thumbs' => array(
				        'thumb' => array(
				            'w' => 190,
				            'h' => 158,
				            'crop' => true,
				        ),
				    ),
				),
				'zip' => array(
				    'dir' => 'webroot{DS}uploads{DS}media{DS}zip'
				),
			),
		);

你必须在数据库中创建一个表

你可以使用模式来做到这一点

cake.php schema create --plugin Attach

或者你可以使用 SQL

CREATE TABLE  `attachments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `filename` varchar(150) NOT NULL,
  `model` varchar(150) NOT NULL,
  `foreign_key` int(11) NOT NULL,
  `type` varchar(100) NOT NULL,
  `size` int(11) NOT NULL,
  `original_name` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

创建你的上传视图,确保它是一个 multipart/form-data 表单,并且文件名字段是 'file' 类型

<?php
		echo $this->Form->create('Media', array('type' => 'file'));
		echo $this->Form->input('name');
		echo $this->Form->input('image', array('type' => 'file'));
		echo $this->Form->input('swf', array('type' => 'file'));
		echo $this->Form->input('zip', array('type' => 'file'));
		echo $this->Form->input('status');
		echo $this->Form->end(__('Submit'));

Attach 会自动为每个你定义的类型创建与模型 Attachment 的关系

		var_dump($this->Media->AttachmentImage);
		var_dump($this->Media->AttachmentSwf);
		var_dump($this->Media->AttachmentZip);

它总是 "Attachment" 加上类型!

许可证

许可协议:[The MIT License](http://krolow.mit-license.org/) 文件重新分配必须保留上述版权声明。

作者

Vinícius Krolow - krolow[at]gmail.com