CakePHP Upload Components Tutorial

Source: Internet
Author: User

1. Copy the plugin to the appropriate directory

Plugin and vendor

2. Add load file to core.php

Require_once dirname (__dir__). '/vendor/autoload.php ';

3.model Add the following two parts

Uploading components
App::uses (' Attachmentbehavior ', ' uploader.model/behavior ');
Cakeplugin::load (' Uploader ');
App::uses (' Vendor ', ' uploader.uploader ');

Public $actsAs = Array (

' Uploader.attachment ' = Array (
Don't copy all these settings, it's merely an example
' Image ' = = Array (
Variables that are used in view files to accept uploaded files
' Finalpath ' = '/files/upload/',//This is a relative path
' Finalpath ' = '/img/uploads/',
' prepend ' = ' upload-',
' NameCallback ' = ' formatname ',
' NameCallback ' = ' formatname ',
' extension ' = = Array (' gif ', ' jpg ', ' png ', ' jpeg '),//acceptable file format, user customizable
' FileSize ' = 5242880,//File size not exceeding 5M
' DbColumn ' = ' image ',//model corresponding to the database field
' Maxnamelength ' = 50,//default is 30
' Overwrite ' = = true,//the same file name will overwrite
' Stopsave ' = true,//If the upload is unsuccessful, automatically stops saving the entire form
' 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 add

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));

Author Address:

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

CakePHP Upload Components Tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.