cakephp上传组件教程

1.复制插件到相应目录

Plugin和Vendor

2.添加加载文件至core.php

require_once dirname(__DIR__) . '/Vendor/autoload.php';

3.model添加以下两部分

//上传组件
App::uses('AttachmentBehavior', 'Uploader.Model/Behavior');
CakePlugin::load('Uploader');
App::uses('Vendor', 'Uploader.Uploader');

 

public $actsAs = array (

'Uploader.Attachment' => array (
// Do not copy all these settings, it's merely an example
'image' => array (
// 在视图文件中用来接受上传文件的变量
'finalPath' => '/files/upload/', // 这个为相对路径
//'finalPath' => '/img/uploads/',
'prepend' => 'upload-',
'nameCallback' => 'formatName',
//'nameCallback' => 'formatName',
'extension' => array('gif', 'jpg', 'png', 'jpeg'), //可接受的文件格式,用户可自定义
'filesize' => 5242880, // 文件大小不超过5M
'dbColumn' => 'image', // model对应数据库的字段
'maxNameLength' => 50, // 默认是30
'overwrite' => true, // 相同文件名时会覆盖
'stopSave' => true, // 如果上传不成功,自动停止保存整个表单
'allowEmpty' => true,
'transforms' => array(
'imageMedium' => array(
'dbColumn' => 'image',
'class' => 'resize',
//'append' => '-medium',
//'overwrite' => true,
'width' => 390,
'height' => 300,
'aspect' => false,
'self' => true
)
)
)
)
);
public function formatName($name, $file) {
return sprintf('%s', date("Y/m/d/G/i/s"));
}

4.view添加

echo $this->Form->create('Yaop', array('type' => 'file',
'inputDefaults' => array(
'div' => false,
'error' => array('attributes' => array('wrap' => 'div', 'class' => 'validate_error'))
)
));

 

echo $this->Form->input('image', array('type'=>'file', 'div' => false, 'label'=>false));

 

 

作者地址:  

http://milesj.me/code/cakephp/uploader

posted @ 2017-06-06 14:52  帽子戏法  阅读(252)  评论(0编辑  收藏  举报
全部 html css js Linux php MySQL seo web git other 联系 管理