2

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

我的视图代码在这里:

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

我的控制器代码在这里:

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

2 回答 2

7

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

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

然后在控制器中做这样的事情:

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

让我知道我是否可以为您提供更多帮助。

于 2013-05-31T03:58:38.133 回答
1

改变

echo $this->Form->create('User',array('enctype'=>'multipart/form-data'));

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

输出

<form id="UserAddForm" enctype="multipart/form-data" method="post" action="/users/add">

于 2013-05-31T03:43:44.960 回答