Gil*_*lko 9 php cakephp file-upload
我想在我的cakephp 3.0应用程序中上传图像.但我收到错误消息:
Notice (8): Undefined index: Images [APP/Controller/ImagesController.php, line 55]
Run Code Online (Sandbox Code Playgroud)
在cakePHP 3.0中是否已经有一些上传文件(一次多个文件)的例子?因为我只能找到cakePHP 2.x的例子!
我想我需要在ImagesTable.php中添加自定义验证方法?但我无法让它发挥作用.
ImagesTable
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;
}
Run Code Online (Sandbox Code Playgroud)
ImagesController
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']);
}
Run Code Online (Sandbox Code Playgroud)
图片add.ctp
<?php
echo $this->Form->input('image_path', [
'label' => 'Image',
'type' => 'file'
]
);
?>
Run Code Online (Sandbox Code Playgroud)
图像实体
protected $_accessible = [
'image_path' => true,
];
Run Code Online (Sandbox Code Playgroud)
Pra*_*dan 11
在您的视图文件中,像我这样添加用户/ dashboard.ctp
<div class="ChImg">
<?php
echo $this->Form->create($particularRecord, ['enctype' => 'multipart/form-data']);
echo $this->Form->input('upload', ['type' => 'file']);
echo $this->Form->button('Update Details', ['class' => 'btn btn-lg btn-success1 btn-block padding-t-b-15']);
echo $this->Form->end();
?>
</div>
Run Code Online (Sandbox Code Playgroud)
在你的控制器中添加像这样,在我的情况下UsersController
if (!empty($this->request->data)) {
if (!empty($this->request->data['upload']['name'])) {
$file = $this->request->data['upload']; //put the data into a var for easy use
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
$setNewFileName = time() . "_" . rand(000000, 999999);
//only process if the extension is valid
if (in_array($ext, $arr_ext)) {
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($file['tmp_name'], WWW_ROOT . '/upload/avatar/' . $setNewFileName . '.' . $ext);
//prepare the filename for database entry
$imageFileName = $setNewFileName . '.' . $ext;
}
}
$getFormvalue = $this->Users->patchEntity($particularRecord, $this->request->data);
if (!empty($this->request->data['upload']['name'])) {
$getFormvalue->avatar = $imageFileName;
}
if ($this->Users->save($getFormvalue)) {
$this->Flash->success('Your profile has been sucessfully updated.');
return $this->redirect(['controller' => 'Users', 'action' => 'dashboard']);
} else {
$this->Flash->error('Records not be saved. Please, try again.');
}
}
Run Code Online (Sandbox Code Playgroud)
在使用此功能之前,请在webroot中创建名为upload/avatar的文件夹.
注意:输入('Name Here')用于
$this->request->data['upload']['name']
Run Code Online (Sandbox Code Playgroud)
如果要查看数组结果,可以打印它.
它的作品就像CakePHP 3.x中的魅力
也许以下内容会有所帮助。这是一个可以帮助您非常轻松地上传文件的行为!
http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/
如果您有困难,请告诉我。
格瑞兹