Yog*_*iya 2 upload cakephp image function
我想从用户表单上传图片.但我的代码不起作用.请帮我
我的观看代码在这里:
echo $this->Form->create('User',array('enctype'=>'multipart/form-data'));
echo $this->Form->input('cat_image',array('type'=>'file'));
echo $this->Form->end('Submit');
Run Code Online (Sandbox Code Playgroud)
我的控制器代码在这里:
move_uploaded_file($_FILES['tmp_name'], WWW_ROOT . 'img/uploads/' . $_FILES['cat_image']);
Run Code Online (Sandbox Code Playgroud)
您必须在表单中添加"类型"标记:
<?php echo $this->Form->create('User', array('type' => 'file'));?>
Run Code Online (Sandbox Code Playgroud)
然后在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'];
}
Run Code Online (Sandbox Code Playgroud)
要像这样查看以在网页中显示图像:
<?php echo $this->Html->image('/path/to/your/image/dir/' . $listShExPainImage['User']['image']); ?>
Run Code Online (Sandbox Code Playgroud)
如果我可以帮助你,请告诉我.