【发布时间】:2017-05-31 00:58:55
【问题描述】:
我正在尝试将图像上传到我的本地文件夹
webroot/img/
并将图像名称保存到数据库。一切似乎都很好,但图像没有保存到路径中
这是我的看法
<?php echo $this->Form->create(null,['url' => ['controller' => 'users', action' => 'update_image'],
array('enctype'=>'multipart/form-data')]); ?>
<?php echo $this->Form->input('User.id') ?>
<?php echo $this->Form->file('Profile.picture',['type'=>'file']) ?>
<?php echo $this->Form->end('submit') ?>
我的控制器
public function update_image(){
$id = $this->Auth->user('id');
$this->Profile->id = $id;
$this->set('profile', $this->User->findById($id));
if ($this->request->is(array('post','put'))) {
$frmData = $this->request->data;
//Path to store upload image
$target = "/teamjob_back/img/../img/".basename($frmData['Profile']['picture']);
//Get the data from form
$image = $frmData['Profile']['picture'];
//save data to database
$this->Profile->save($this->request->data);
//Image store to the img folder
if (move_uploaded_file($image['tmp_name']['name'], $target)) {
echo "Image successfull";
}
}
}
还有代码
$image['tmp_name']['name']
给我一个非法字符串偏移错误。
已编辑 此代码适用于控制器
if ($this->request->is('post')) {
$frmData = $this->request->data;
$tmp = $frmData['picture']['tmp_name'];
$hash = rand();
$date = date("Ymd");
$image = $date.$hash."-".$frmData['picture']['name'];
$target = WWW_ROOT.'img'.DS.'uploads'.DS;
$target = $target.basename($image);
if (move_uploaded_file($tmp, $target)) {
echo "Successfully moved";
}
else
{
echo "Error";
}
}
【问题讨论】:
-
<?= $this->Form->create(NULL, ['enctype' => 'multipart/form-data']) ?>创建表单的时候加enctype了吗? -
<form action="/teamjob_back/users/picture" enctype="multipart/form-data" id="UserPictureForm" method="post" accept-charset="utf-8"> -
该代码有效
标签: php cakephp cakephp-2.9