gpt4 book ai didi

php - cakePHP 3.0 上传图片

转载 作者:可可西里 更新时间:2023-10-31 23:08:40 26 4
gpt4 key购买 nike

我想在我的 cakephp 3.0 应用程序中上传图像。但我收到错误消息:

Notice (8): Undefined index: Images [APP/Controller/ImagesController.php, line 55]

在 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;
}

图像 Controller

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'
]
);
?>

图像实体

protected $_accessible = [
'image_path' => true,
];

最佳答案

在你的 View 文件中,像这样添加,在我的例子中是 Users/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>

在你的 Controller 中添加这样的,在我的例子中是 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.');
}
}

在使用它之前,在 webroot 中创建一个名为 upload/avatar 的文件夹。

注意:输入('Name Here'),用在

$this->request->data['upload']['name']

如果你想看到数组结果,你可以打印它。

它在 CakePHP 3.x 中的运行就像一个魅力

关于php - cakePHP 3.0 上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29648335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com