我試圖將圖像上傳到我的本地文件夾CakePHP的2X上傳圖像
根目錄/ 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']
給了我一個非法串偏移誤差。
EDITED 此代碼工作在控制器上
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->形式 - >創建(NULL,['加密類型,包括加密類型'=>'multipart/form-data'])?>你在創建表單時是否添加了enctype? –
'
該代碼有效 –