問(wèn)題描述
我想在我的 cakephp 3.0 應(yīng)用程序中上傳圖片.但我收到錯(cuò)誤消息:
I want to upload images in my cakephp 3.0 app. But I get the error message:
Notice (8): Undefined index: Images [APP/Controller/ImagesController.php, line 55]
是否已經(jīng)有一些在 cakePHP 3.0 中上傳文件(一次多個(gè)文件)的示例?因?yàn)槲抑荒苷业?cakePHP 2.x 的示例!
Are there already some examples for uploading files (multiple files at once) in cakePHP 3.0? Because I can only find examples for cakePHP 2.x !
我想我需要在我的 ImagesTable.php 中添加一個(gè)自定義驗(yàn)證方法?但我無(wú)法讓它工作.
I think I need to add a custom validation method in my ImagesTable.php? But I can't get it to work.
圖像表
public function initialize(array $config) {
$validator
->requirePresence('image_path', 'create')
->notEmpty('image_path')
->add('processImageUpload', 'custom', [
'rule' => 'processImageUpload'
])
}
public function processImageUpload($check = array()) {
if(!is_uploaded_file($check['image_path']['tmp_name'])){
return FALSE;
}
if (!move_uploaded_file($check['image_path']['tmp_name'], WWW_ROOT . 'img' . DS . 'images' . DS . $check['image_path']['name'])){
return FALSE;
}
$this->data[$this->alias]['image_path'] = 'images' . DS . $check['image_path']['name'];
return TRUE;
}
圖像控制器
public function add()
{
$image = $this->Images->newEntity();
if ($this->request->is('post')) {
$image = $this->Images->patchEntity($image, $this->request->data);
$data = $this->request->data['Images'];
//var_dump($this->request->data);
if(!$data['image_path']['name']){
unset($data['image_path']);
}
// var_dump($this->request->data);
if ($this->Images->save($image)) {
$this->Flash->success('The image has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The image could not be saved. Please, try again.');
}
}
$images = $this->Images->Images->find('list', ['limit' => 200]);
$projects = $this->Images->Projects->find('list', ['limit' => 200]);
$this->set(compact('image', 'images', 'projects'));
$this->set('_serialize', ['image']);
}
圖片添加.ctp
<?php
echo $this->Form->input('image_path', [
'label' => 'Image',
'type' => 'file'
]
);
?>
圖像實(shí)體
protected $_accessible = [
'image_path' => true,
];
推薦答案
也許以下內(nèi)容會(huì)有所幫助.這是一種可以幫助您輕松上傳文件的行為!
Maybe the following would help. It's a behavior who helps you to upload files very easy!
http://cakemanager.org/docs/utils/1.0/behaviors/可上傳/
如果您遇到困難,請(qǐng)告訴我.
Let me know if you struggle.
問(wèn)候
這篇關(guān)于cakePHP 3.0 上傳圖片的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!