1. 程式人生 > >cakephp上傳組件教程

cakephp上傳組件教程

all res size att 文件的 相對路徑 clas 相同 rep

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

cakephp上傳組件教程