Cakephpw2.4.5 с помощью загрузчика MilesJohnson не может загрузить фото

Я хочу использовать это для загрузки фотографий, но в БД хранились только имя и строка фотографии. Может кто-нибудь сказать, что я могу сделать, чтобы исправить ошибку, чтобы сделать загрузку фотографий в мою папку?

Я вставляю свой контроллер, модель, добавляю код сюда.

и моя схема таблицы: table: images id: int str: text img: varchar

Спасибо

<?php
App::uses('AppController', 'Controller');
App::import('Vendor', 'Uploader.Uploader');
CakePlugin::load('Uploader');
class ImagesController extends AppController {
public $components = array('Paginator');

public function index() {
    $this->Image->recursive = 0;
    $this->set('images', $this->Paginator->paginate());
}

public function view($id = null) {
    if (!$this->Image->exists($id)) {
        throw new NotFoundException(__('Invalid image'));
    }
    $options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id));
    $this->set('image', $this->Image->find('first', $options));
}
public function add() {
    if ($this->request->is('post')) {
        $this->Image->create();
        if ($this->Image->save($this->request->data)) {
            $this->Session->setFlash(__('The image has been saved.'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The image could not be saved. Please, try again.'));
        }
    }
}

public function edit($id = null) {
    if (!$this->Image->exists($id)) {
        throw new NotFoundException(__('Invalid image'));
    }
    if ($this->request->is(array('post', 'put'))) {
        if ($this->Image->save($this->request->data)) {
            $this->Session->setFlash(__('The image has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The image could not be saved. Please, try again.'));
        }
    } else {
        $options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id));
        $this->request->data = $this->Image->find('first', $options);
    }
}

public function delete($id = null) {
    $this->Image->id = $id;
    if (!$this->Image->exists()) {
        throw new NotFoundException(__('Invalid image'));
    }
    $this->request->onlyAllow('post', 'delete');
    if ($this->Image->delete()) {
        $this->Session->setFlash(__('The image has been deleted.'));
    } else {
        $this->Session->setFlash(__('The image could not be deleted. Please, try again.'));
    }
    return $this->redirect(array('action' => 'index'));
}}

?>

и следующее модель

<?php
App::uses('AppModel', 'Model');
App::uses('AttachmentBehavior', 'Uploader.Model/Behavior');
CakePlugin::load('Uploader');
class Image extends AppModel {
//from Polin
public $name = 'Image';
public $actsAs = array(
    'Uploader.Attachment' => array(
        'image' => array(
            'nameCallback' => 'formatName',
            'tempDir' => TMP,
            'uploadDir' => 'C:\\wamp\\www\\huahei224\\app\\webroot\\img\\uploads', 
            'overwrite' => false,
            'stopSave' => true,
            'allowEmpty' => true,
            'transforms' => array(
                'image_md' => array(
                    'nameCallback' => 'transformNameCallback',
                    'class' => 'resize',
                    'append' => '-md',
                    'overwrite' => true,
                    'width' => 400,
                    'height' => 300,
                    'aspect' => true
                ),
                'image_sm' => array(
                    'nameCallback' => 'transformNameCallback',
                    'class' => 'crop',
                    'append' => '-sm',
                    'overwrite' => true,
                    'self' => false,
                    'width' => 150,
                    'height' => 150
                )
            )
        )
    ),
   /** 'Uploader.FileValidation' => array(
        'image' => array(
            'minHeight' => 164,
            'extension' => array('gif', 'jpg', 'png', 'jpeg'),
            'type' => 'image',
            'filesize' => 5242880,
            'required' => false
        )
    )
    **/
);
public function formatName($name, $file) {
    return sprintf('%s', uniqid());
}
public function transformNameCallback($name, $file) {
    return $this->getUploadedFile()->name();
}   
public $validate = array(
    'img' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),

    'str' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);
}

и последний из них - add.ctp

<div class="images form">
<?php echo $this->Form->create('Image'); ?>
<fieldset>
    <legend><?php echo __('Add Image'); ?></legend>
<?php
    //echo $this->Form->input('img');
    echo $this->Form->create('Image', array('type' => 'file'));
    echo $this->Form->input('str',array('label' => '文字說明'));
?>

<?php
    echo $this->Form->input('img', array('type' => 'file','label' => 'upload photos')); 
    //echo $this->Form->end('Submit');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>

    <li><?php echo $this->Html->link(__('List Images'), array('action' => 'index')); ?></li>
</ul>

1 ответ

Попробуйте добавить следующее:

<?php echo $this->Form->create('Image', array('enctype' => 'multipart/form-data')); ?>

это требуется для форм, которые содержат загрузки, см.: Файл FormHelper

Другие вопросы по тегам