cakephp上傳組件教程

來源:互聯網
上載者:User

標籤:jpg   orm   教程   ops   成功   自訂   height   upload   date   

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上傳組件教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.