我想用它来上传照片,但只有照片的名称和字符串存储在数据库中。 任何人都可以告诉我该怎么做才能纠正错误,让照片上传到我的文件夹?
我粘贴我的控制器,模型,在这里添加代码。
我的表架构是: 表:图像 id:int str:文字 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>
答案 0 :(得分:0)
尝试添加以下内容:
<?php echo $this->Form->create('Image', array('enctype' => 'multipart/form-data')); ?>
这对于包含上传的表单是必需的,请参阅: FormHelper File