2013-05-31 51 views
2

我想從用戶表單上傳圖片。但我的代碼不起作用。請幫我在Cakephp上傳圖片

我的看法代碼是在這裏:

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

回答

1

變化

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

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

輸出

<表格ID = 「UserAddForm」 ENCTYPE = 「多部分/格式數據」 方法= 「POST」 行動= 「/用戶/添加」 >

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']; 
} 

,並查看這樣來顯示webpag圖像e:

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

讓我知道,如果我可以幫助你更多。