我使用组件名称upload.php并将其保存在/ app / controllers / components /目录中。
之后我在我的控制器中使用了代码
<?php
App::uses('AppController', 'Controller');
App::uses('BarcodeHelper','Vendor');
/**
* OesUsers Controller
*
* @property OesUser $OesUser
*/
class OesUsersController extends AppController {
var $name = 'Images';
var $helpers = array('Html', 'Form');
var $components = array('upload');
function upload() {
if (empty($this->data)) {
$this->render();
} else {
$this->cleanUpFields();
// set the upload destination folder
$destination = realpath('../../app/webroot/img/uploads/') . '/';
// grab the file
$file = $this->data['Image']['filedata'];
// upload the image using the upload component
$result = $this->Upload->upload($file, $destination, null, array('type' => 'resizecrop', 'size' => array('400', '300'), 'output' => 'jpg'));
if (!$result){
$this->data['Image']['filedata'] = $this->Upload->result;
} else {
// display error
$errors = $this->Upload->errors;
// piece together errors
if(is_array($errors)){ $errors = implode("<br />",$errors); }
$this->Session->setFlash($errors);
$this->redirect('/images/upload');
exit();
}
if ($this->Image->save($this->data)) {
$this->Session->setFlash('Image has been added.');
$this->redirect('/images/index');
} else {
$this->Session->setFlash('Please correct errors below.');
unlink($destination.$this->Upload->result);
}
}
}
保存后,我发现缺少组件的错误。我找不到任何错误。任何人都可以帮助我吗?
答案 0 :(得分:2)
您已将组件放在错误的文件夹中。在CakePHP 2.x中,组件放在app/Controller/Component
中。您还必须将组件从upload.php
重命名为UploadComponent
(并且可能会调整代码以使其与CakePHP 2.x一起使用)。