gpt4 book ai didi

在 Cakephp 中上传图片

转载 作者:行者123 更新时间:2023-12-02 07:03:15 25 4
gpt4 key购买 nike

我想从用户表单上传图片。但是我的代码不起作用。请帮助我

我的查看代码在这里:

echo $this->Form->create('User',array('enctype'=>'multipart/form-data'));
echo $this->Form->input('cat_image',array('type'=>'file'));
echo $this->Form->end('Submit');

我的 Controller 代码在这里:

move_uploaded_file($_FILES['tmp_name'], WWW_ROOT . 'img/uploads/' . $_FILES['cat_image']);

最佳答案

您必须在表单中添加“type”标签:

<?php echo $this->Form->create('User', array('type' => 'file'));?>

然后在 Controller 中做这样的事情:

if (is_uploaded_file($data['User']['image']['tmp_name']))
{
move_uploaded_file(
$this->request->data['User']['image']['tmp_name'],
'/path/to/your/image/dir/' . $this->request->data['User']['image']['name']
);

// store the filename in the array to be saved to the db
$this->request->data['User']['image'] = $this->request->data['User']['image']['name'];
}

然后像这样查看以在网页中显示图像:

<?php echo $this->Html->image('/path/to/your/image/dir/' . $listShExPainImage['User']['image']); ?>

如果我能为您提供更多帮助,请告诉我。

关于在 Cakephp 中上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16848737/

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